2021.7.13
自己的两次代码都贴出来吧
第一种:
#include<stdio.h>
#include<math.h>
int gcd(int a,int b)
{
int r;
while((r=a%b)!=0)
{
a=b;
b=r;
}
return b;
}
int main()
{
int t,q,p;
while(scanf("%d %d",&q,&p)!=EOF)
{
if(q>p)
{
t=q;
q=p;
p=t;
}
t=gcd(q,p);
q/=t; p/=t;
printf("%d\n",(p+(p-q)*(q-1))*t);
}
}
很笨比的一种做法...思路大概是先按照多的分,在从多的里面找出两数的差值再分一次,特地的还都约分了一下,但是实际上还有很多情况没照顾的,毫无疑问的WA了
第二种:
#include<stdio.h> #include<math.h> int gcd(int a,int b) { int r; while((r=a%b)!=0) { a=b; b=r; } return b; } int main() { int t,q,p; while(scanf("%d %d",&q,&p)!=EOF) { if(q>p) { t=q; q=p; p=t; } t=gcd(p,q); printf("%d\n",q+p-t); } }
第二种也是在csdn上找的,思路清晰的多,大概意思是想象一个图形(圆或者长矩形都可以),就按着给的数切割,在两次切割的时候会出现一些重和的部分,那些重合的线的数量就是最大公约数(比我想的简单多了,还不用化简...)这样子想就清晰多了
没做出来确实是自己思考少了