## #10994- Need help

Moderator: Board moderators

### #10994- Need help

The solution is correct but it exceeds time limit so what can I do about it? Please tell me anyone. Here is my solution:

#include <stdio.h>

int fu(int ab)
{

if(ab%10>0 && ab>0)
{
return ab%10;
}
else
{
return ab/10;
}
}

int S(int a,int b)
{
int ans=0,i;
for(i=a;i<=b;i++)
{
ans+=fu(i);

}
printf("%d", ans);
return 0;
}

int main()
{
int in1,in2,ans;
while((scanf("%d %d",&in1,&in2)==2) && (in1!=-1 && in2!=-1))
{

if(in1>in2)
{
S(in2,in1);

}
else
{
S(in1,in2);

}

}
return 0;

}
dotnet12
New poster

Posts: 4
Joined: Fri Aug 17, 2012 9:43 pm

### Re: #10994- Need help

Brute force won't work here, you'll need to come up with a faster solution.
brianfry713
Guru

Posts: 1771
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: #10994- Need help

can u give 1?
dotnet12
New poster

Posts: 4
Joined: Fri Aug 17, 2012 9:43 pm

### Re: #10994- Need help

If you were asked to simply sum all the integers between p and q, would you loop? Is there a faster way?
brianfry713
Guru

Posts: 1771
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Who is online

Users browsing this forum: No registered users and 0 guests