2018年1月12日 星期五

[MM19-易] Surface to Volume Ratio(C++)

[MM19-易] Surface to Volume Ratio

Problem DescriptionPicture01
Write a program to compute surface to volume ratio. We will be given the height, width, and depth of N cuboid, and determine the smallest surface to volume ratio among them.
Input File Format
The first line of the input data consists of N, where 0 < N <= 1000. The next N lines contain the height, width, and depth of each cuboid. All the dimensions are between 1 and 50.
Output Format
You should output the smallest surface to volume ratio as "a/b". This number must be simplified, that is, a and b must be prime to each other.
Example

Sample Input:Sample Output:
2
2 3 4
1 1 1
13/6
  1. #include <iostream>  
  2. #include <string>  
  3. #include <sstream>  
  4. #include <stdio.h>  
  5. #include <ctype.h>  
  6. #include <cmath>  
  7. using namespace std;  
  8.   
  9. int main() {  
  10.     // [MM19-易] Surface to Volume Ratio  
  11.     int n;  
  12.     int a, b, c;  
  13.     int mvol = 0, msur = 0;  
  14.     cin >> n;  
  15.     double min = 125000;  
  16.     for(int i = 0;i < n;i++)  
  17.     {  
  18.         cin >> a >> b >> c;  
  19.         int vol = a*b*c;  
  20.         int sur = (a*b+b*c+a*c)*2;  
  21.         double re = (double)sur/(double)vol;  
  22.         if(re < min)  
  23.         {  
  24.             min = re;  
  25.             mvol = vol;  
  26.             msur = sur;  
  27.         }  
  28.     }  
  29.       
  30.     for(int i = 1;i < mvol;i++)  
  31.     {  
  32.         if(mvol % i == 0 && msur % i == 0)  
  33.         {  
  34.             if(i != 1)  
  35.             {  
  36.                 while(mvol % i == 0 && msur % i == 0)  
  37.                 {  
  38.                     mvol = mvol / i;  
  39.                     msur = msur / i;  
  40.                 }  
  41.             }  
  42.         }  
  43.     }  
  44.     cout << msur << "/" << mvol << endl;  
  45.     return 0;  
  46. }  

沒有留言:

張貼留言