Problem 1028 - Counting Beans

/*

Time Limit: 1000MS   Memory Limit: 65536KB 

Difficulty:
Total
Submit: 107  Accepted: 53  Special Judge: No DescriptionWhat 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.
InputThere 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.OutputFor each case, output one line with a single integer describes the
maximum piles she can get.

*/

题意大概是分红豆绿豆,然后尽可能多的把红绿豆分成相等的堆。就是求最大公约数。欧几里得算法的递归写法比较简单,代码就两行。

 1 #include<stdio.h>
 2 int gcd(int a,int b)
 3 {
 4     if(b==0) return a;
 5     else return gcd(b,a%b);
 6 }
 7 int  main()
 8 {
 9     int a,b;
10     while(scanf("%d%d",&a,&b)!=EOF&&a+b!=0)
11     {
12         printf("%d\n",gcd(a,b));
13     }
14     return 0;
15 }

 

posted @ 2013-08-10 19:46  hjf007  阅读(195)  评论(0编辑  收藏  举报