hdu1722(gcd)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1722

题意:要使一块蛋糕既能均分给a个人,又能均分给b个人,问至少需要分成几块(不需要每块都一样大小);

思路:假设先将蛋糕切c=gcd(a,b)块,那么再将每块分成a/c块可以可以得到a块蛋糕,再将蛋糕合起来,然后分成b块,这次分的过程中会有c块蛋糕是之前切好的,也就是总共要分成a+b-gcd(a, b)块;

代码:

 #include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std; int main(void){
int a, b;
while(scanf("%d%d", &a, &b)!=EOF){
int ans = a + b - __gcd(a, b);
printf("%d\n", ans);
}
return 0;
}
上一篇:html5 touch事件实现触屏页面上下滑动(二)


下一篇:Intel MKL函数,如何得到相同的计算结果?【转】