HDU 5640 King's Cake

本文介绍了一个有趣的算法问题——国王的蛋糕。国王需要将一个n*m大小的矩形蛋糕切成尽可能多的正方形蛋糕,每次只能切下一个正方形。文章提供了一种解决方法,并附带了C++代码实现。

King's Cake

Problem Description
It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1n,m10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time, he will cut the rectangle cake into two pieces, one of which should be a square cake.. Since he loves squares , he will cut the biggest square cake. He will continue to do that until all the pieces are square. Now can you tell him how many pieces he can get when he finishes.
 
Input
The first line contains a number T(T1000), the number of the testcases.
For each testcase, the first line and the only line contains two positive numbers n,m(1n,m10000).
 
Output
For each testcase, print a single number as the answer.
 
Sample Input
2
2 3
2 5
 
Sample Output
3
4
 
Recommend
wange2014
题目大意:阅兵式前一天,是国王的生日,大臣们为他准备了一个 n *m(1<=n, m<=10000)的蛋糕。他准备切蛋糕,但他切蛋糕有奇奇怪怪的癖好,他每次只切一刀,切下一个正方形蛋糕。请问它最多能切出多少个正方形蛋糕?
思路:看图
 
#include <iostream>
#include <cstdio>
using namespace std;
int t;
int f(int m, int n)
{
        if(m<n) return (f(n,m));
        t += m/n;
        if(m%n==0) return 0;
        f(n,m%n);
}
int main()
{
        int m, n, T;
        scanf("%d", &T);
        while(T--)
        {
        scanf("%d%d", &m, &n);
        t=0;
        f(m, n);
        printf("%d\n", t);
        }
        return 0;
}

 

转载于:https://www.cnblogs.com/Noevon/p/5425955.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值