2018年1月12日 星期五

[MM68-易] Fill the Bowl(C++)

[MM68-易] Fill the Bowl

Problem Description
You have two empty bowls with volume a and units (a and b are positive integers). You want to design a bottle, so that you can fill each bowl with water `perfectly’ as follows:
(i) Fill the bottle fully with water.
(ii) Empty the bottle completely by pouring the water to the bowl, and no water should get out of the bowl.
(iii) Repeat (i) and (ii) if the bowl is not full.
You hope to design a bottle with as large volume as possible, so that you can fill a bowl in fewer rounds. However, no water should be wasted in the above filling process (that is, when emptying the bottle in (ii), no water should get out of the bowl), and due to some limitation, the volume of the bottle is at most Lunits. How large a bottle can you design?
Input File Format
Each test case consists of a line containing three positive integers, ab, and L, with abL <= 1000.
Output Format
For each test case, output in a line the maximum volume of the bottle.
Example

Sample Input:Sample Output:
6 8 3
4 11 10
9 36 5
2
1
3

  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <string>  
  4. #include <sstream>  
  5. #include <stdio.h>  
  6. #include <ctype.h>  
  7. #include <cmath>  
  8. using namespace std;  
  9.   
  10. int main() {  
  11.     // [MM68-易] Fill the Bowl  
  12.     int a, b, L;  
  13.     while(cin >> a >> b >> L)  
  14.     {  
  15.         if(a < b)  
  16.         {  
  17.             for(int i = a;i > 0;i--)  
  18.             {  
  19.                 if(b % i == 0 && a % i == 0 && i <= L)  
  20.                 {  
  21.                     cout << i << endl;  
  22.                     break;  
  23.                 }  
  24.             }  
  25.         }  
  26.         else //a > b  
  27.         {  
  28.             for(int i = b;i > 0;i--)  
  29.             {  
  30.                 if(b % i == 0 && a % i == 0 && i <= L)  
  31.                 {  
  32.                     cout << i << endl;  
  33.                     break;  
  34.                 }  
  35.             }  
  36.         }  
  37.     }  
  38.     return 0;  
  39. }  

沒有留言:

張貼留言