由题意推得结论:p+q-gcd(p,q);
/* * hdu 1722--Cake * date 2014/7/15 * state AC */ #include <iostream> #include <cstdio> using namespace std; /* int gcd(int x,int y) { while(x!=y) { if(x>y)x=x-y; else y=y-x; } return x; } */ int gcd(int x,int y) { if(x<y) return gcd(y,x); if(y==0) return x; else return gcd(x-y,y); } int main() { //cout << "Hello world!" << endl; int p,q; while(scanf("%d %d",&p,&q)!=EOF) { cout<<p+q-gcd(p,q)<<endl; } return 0; }
原文:http://blog.csdn.net/greenapple_shan/article/details/37784111