11417 - GCD
Moderator: Board moderators
-
- New poster
- Posts: 9
- Joined: Mon Aug 13, 2007 7:45 am
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
your GCD function doesn't return if x!=0 && y!=0.
changeto
change
Code: Select all
if(x<y)GCD(x,y%x);
else GCD(y,x%y);
Code: Select all
if(x<y)return GCD(x,y%x);
else return GCD(y,x%y);
-
- New poster
- Posts: 9
- Joined: Mon Aug 13, 2007 7:45 am
Yes, you are right! thanks a lot!emotional blind wrote:your GCD function doesn't return if x!=0 && y!=0.
changetoCode: Select all
if(x<y)GCD(x,y%x); else GCD(y,x%y);
Code: Select all
if(x<y)return GCD(x,y%x); else return GCD(y,x%y);
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
Re: 11417 - GCD
I am new coder and trying to learn. Can any 1 help me?
I am got wa in the problem 11417 - GCD . I don't know what is the error.
here is my code..
I am got wa in the problem 11417 - GCD . I don't know what is the error.
here is my code..
Code: Select all
#include<stdio.h>
int gcd( int a, int b )
{
if( b == 0 ) return a;
else gcd( b, a % b );
}
int main()
{
int i,N,j;
long G=0;
while(scanf("%d",&N)==1)
{
if(N==0)
{
break;
}
G=0;
for(i=1;i<N;i++)
{
for(j=i+1;j<=N;j++)
{
G+=gcd(i,j);
}
}
if(N==1)
{
G=1;
}
printf("%ld\n",G);
}
return 0;
}
Re: 11417 - GCD
your solution is wrong because of this part of the code:
you should change it for this one:
and maybe your solution get TLE because of the recursive function gcd, good luck
Code: Select all
if(N==1)
{
G=1;
}
Code: Select all
if(N==1)
{
G=0;
}
Re: 11417 - GCD
can i solve 11424 in this way.....
if i give same code .... verdict is time limit......
if i give same code .... verdict is time limit......
11417 GCD
what is the problem of this code???
it works for input 10,100 and less than 209 but it doesn't work when input is 210+ ....
here is the code link:
http://ideone.com/pxAuWn
plz someone help meeee....![:-?](./images/smilies/icon_confused.gif)
it works for input 10,100 and less than 209 but it doesn't work when input is 210+ ....
here is the code link:
http://ideone.com/pxAuWn
plz someone help meeee....
![:-?](./images/smilies/icon_confused.gif)
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11417 GCD
Check input and AC output for thousands of problems on uDebug!