B
来源:互联网 发布:手机淘宝发货地怎么改 编辑:程序博客网 时间:2024/04/30 12:33
一次生日Party可能有p人或者q人参加,现准备有一个大蛋糕.问最少要将蛋糕切成多少块(每块大小不一定相等),才能使p人或者q人出席的任何一种情况,都能平均将蛋糕分食.
Input
每行有两个数p和q.
Output
输出最少要将蛋糕切成多少块.
Sample Input
2 3
Sample Output
4
Hint
将蛋糕切成大小分别为1/3,1/3,1/6,1/6的四块即满足要求.
当2个人来时,每人可以吃1/3+1/6=1/2 , 1/2块。
当3个人来时,每人可以吃1/6+1/6=1/3 , 1/3, 1/3块。
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;int gcd(int a,int b){ int c=a%b; if(c==0) { return b; } else { a=b; b=c; return gcd(a,b); }}int main(){ int p,q; while(cin>>p>>q) { int ans=p+q-gcd(p,q); cout<<ans<<endl; } return 0;}
阅读全文
0 0