Description
What a day! It's dark outside, and Tiantian has nothing to do. She's boring and starts to count beans. There're two kinds of beans, red bean and mung bean. It doesn't take long, she becomes boring again. So she finds another job to do: dividing the beans into as many piles as she can! And, of course, each pile must have the same number of red beans and mung beans. For example, if she has 6 red beans and 4 mung beans, she can divide them into 2 piles, and each pile will contain 3 red beans and 2 mung beans. It's so difficult for her to solve it, so she comes to ask you to program it.
Input
There are multiple test cases. Each case takes one line and contains two integers r and m(1≤r≤2^31-1, 1≤m≤2^31-1) separated by a single space. They describe the number of red beans and mung beans separately.
r=0,m=0 indicates the end of the input and should not be processed by your program.
Output
For each case, output one line with a single integer describes the maximum piles she can get.
Sample Input
6 4
5 3
0 0
Sample Output
2
1
解题思路:
gcd
0<=m<n
gcd(0,n) = n
gcd(m,n) = gcd(n mod n,m)m>0
gcd*lcd = m*n
#include<iostream>
using namespace std;
void swap(int &r1,int &r2)
{
int temp = r1;
r1 = r2;r2 = temp;
}
int gcd(int x,int y)
{
if(x==0)
return y;
else
return gcd(y%x,x);
}
int main()
{
int m,n;
while(cin>>m>>n)
{
if(m==0&&n==0)
break;
else
{
if(m>n)
swap(m,n);
cout<<gcd(m,n)<<endl;
}
}
return 0;
}
最后欢迎大家访问我的个人网站: 1024s