您的位置:首页 > 其它

HDOJ 5640 king's cake

2016-03-19 11:46 232 查看
[align=left]Problem Description[/align]
It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size
n×m(1≤n,m≤10000)


. 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.

[align=left]Input[/align]
The first line contains a number
T(T≤1000)


, the number of the testcases.

For each testcase, the first line and the only line contains two positive numbers
n,m(1≤n,m≤10000)


.

[align=left]Output[/align]
For each testcase, print a single number as the answer.

[align=left]Sample Input[/align]

2
2 3
2 5


[align=left]Sample Output[/align]

3
4

hint:
For the first testcase you can divide the into one cake of $2\times2$ , 2 cakes of $1\times 1$[code]#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int sum;
void cut (int n,int m)
{
if(n!=m)
{
if(n>m)
{
int temp=n;
n=m;
m=temp;
}
sum++;
cut(n,m-n);
}

}
int main()
{
int T;
cin>>T;
for(int k=1;k<=T;k++)
{
int n,m;
sum=0;
cin>>n>>m;
cut(n,m);
cout<<sum+1<<endl;
}
return 0;
}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: