有趣问题:长方形不断分割掉最大正方形(5640)

来源:互联网 发布:管理电脑桌面的软件 编辑:程序博客网 时间:2024/04/28 23:09

King's Cake

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


Problem Description
It is the king's birthday before the military parade . The ministers prepared a rectangle cake of sizen×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 numbersn,m(1n,m10000).
 

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

Sample Input
22 32 5
 

Sample Output
34

求解过程很像求两数的最大公约数。


/*------------------Header Files------------------*/#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <algorithm>#include <cstdlib>#include <ctype.h>#include <cmath>#include <stack>#include <queue>#include <deque>#include <map>#include <vector>#include <limits.h>using namespace std;/*------------------Definitions-------------------*/#define LL long long#define PI acos(-1.0)#define INF 0x3F3F3F3F#define MOD 10E9+7#define MAX 500050/*---------------------Work-----------------------*/void work(){int T; cin>>T;while(T--){int n,m;scanf("%d%d",&n,&m);int a,b,cnt=0;while(1){a=max(m,n),b=min(m,n);cnt+=a/b;if(a%b==0) break;m=a-a/b*b,n=b;}printf("%d\n",cnt);}}/*------------------Main Function------------------*/int main(){//freopen("test.txt","r",stdin);//freopen("cowtour.out","w",stdout);//freopen("cowtour.in","r",stdin);work();return 0;}



0 0
原创粉丝点击