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 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) .
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
求解过程很像求两数的最大公约数。
/*------------------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;
}