XDOJ1028 - Counting Beans

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值