C语言关于求两个数的共同因子问题!!!
4128: Tao God and Jiong King IITime Limit: 1 Sec Memory Limit: 128 MB
Submit: 303 Solved: 22
Description
Today Tao God invite Jiong King to join his party of heaven.It’s wellknown to us,Tao God is the leader of heaven and Jiong King is the leader of hell.To welcome for Jiong King’coming,Tao God come up with a game to play with Jiong King.Tao God select a number A and Jiong King select a number B.Their value of happiness is the sum of the common divisors of A and B.
If X can be divided by Y, we call Y is a divisor of X. For example, 3 is a divisor of 6.Now, give you Tao God’selection A and Jiong King’selection B,please help them calculate their value of happiness.
Input
The input file consists of multipe test cases.
Each tast case only contains two numbers A and B.
(1 <= A, B <= 100000000)
Output
Output their value of happiness.
Sample Input
5 10
6 8
Sample Output
6
3
HINT
我写的代码,交上去是超时的。。。这道题要用什么方法解?
#include<stdio.h>
#include<string.h>
int main()
{
long int A,B,t,i;
int s;
while(scanf("%d%d",&A,&B)!=EOF)
{
if(A>B)
{A=t;A=B;B=t;}
s=0;
for(i=1;i<=A;i++)
{
if(A%i==0&&B%i==0)
{
s+=i;
}
}
printf("%d\n",s);
}
return 0;
}