HDU5640(水)

来源:互联网 发布:发票网络认证系统 编辑:程序博客网 时间:2024/05/29 00:32

题目:

King’s Cake

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1105 Accepted Submission(s): 768

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

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

Output
For each testcase, print a single number as the answer.

Sample Input
2
2 3
2 5

Sample Output
3
4

hint:
For the first testcase you can divide the into one cake of 2×2 , 2 cakes of 1×1

水题。尴尬的是,3个小时内一直不知道错哪里,上来直接用脑子里的思路写了,结果超时,(不考虑数据范围。。),然后换思路,每次将长缩短宽的倍数,结果除0错误,修改后,继续WA,再读题,发现是positive number,正数,包含正浮点数,(PS:正整数:positive integer),改了再交,结果还是WA,这时候我觉得我已经全部考虑了一遍,很无奈,先做其他题。中途回来,发现漏了一个条件,判断长和宽相等,excuse me???The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000),年轻的我一直认为是rectangle,然后我改了,改成了if(n==m){cout<<0<<endl; continue;}
我的天,离AC只有一步,过了半个小时,我终于发现应该为if(n==m){cout<<1<<endl; continue;}

小小的总结一下,在WA了之后,应该从头到尾重新考虑一遍,从中间入手,十分不好

代码:

#include <iostream>using namespace std;int main(){    int t;    double n, m;    cin>>t;    while(t--){        cin>>n>>m;        int cnt = 0;        if(n==m) {cout<<1<<endl; continue;}        while(m!=n&&m*n>0){            if(n<m) swap(n, m);            //cout<<"m is: "<<m<<" "<<"n is:"<<n<<endl;            int temp = n/m;            n -= m*temp*1.0;            //cout<<"temp is:"<<temp<<endl;            cnt+=temp;        }        cout<<cnt<<endl;    }    return 0;}
0 0
原创粉丝点击