#10994- Need help

All about problems in Volume CIX. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

#10994- Need help

Postby dotnet12 » Fri Aug 17, 2012 9:49 pm

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

Postby brianfry713 » Sat Aug 18, 2012 1:29 am

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

Postby dotnet12 » Sat Aug 18, 2012 3:10 am

can u give 1? :D :D
dotnet12
New poster
 
Posts: 4
Joined: Fri Aug 17, 2012 9:43 pm

Re: #10994- Need help

Postby brianfry713 » Mon Aug 20, 2012 10:13 pm

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


Return to Volume CIX

Who is online

Users browsing this forum: No registered users and 0 guests