King's Cake
Accepts: 960
Submissions: 1572
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
问题描述
阅兵式前一天,是国王的生日,大臣们为他准备了一个 n×m(1≤n,m≤10000) 的蛋糕。他准备切蛋糕,但他切蛋糕有奇奇怪怪的癖好,他每次只切一刀,切下一个正方形蛋糕。请问它最多能切出多少个正方形蛋糕?
输入描述
第一行一个整数表示测试组数:T(0<T≤1000) 。 每组数据占一行,每行两个整数 n×m(1≤n,m≤10000),表示蛋糕的大小。
输出描述
共 T 行,每行一个整数表示最多能切出的正方形蛋糕数量。
输入样例
2 2 3 2 5
输出样例
3 4
Hint
对于第一组数据,可切出一个 2×2, 两个 1×1,共 3 个。 对于第一组数据,可切出两个 2×2, 两个 1×1,共 4 个。
题解:直接模拟
代码:
#include <cstdio> #include <algorithm> #include <cmath> using namespace std; int main() { #ifdef CDZSC_June freopen("t.txt","r",stdin); #endif //CDZSC_June int t,n,m; scanf("%d",&t); while(t--) { int sum = 0,p,k; scanf("%d%d",&n,&m); if(n == m){ puts("1"); } else { int max1 = max(n,m), min1 = min(n,m); while(min1>=1) { sum += max1/min1; p = max1,k = min1; max1 = max(p%k,k); min1 = min(p%k,k); } printf("%d\n",sum); } } return 0; }