2017年12月16日 星期六

[ITSA Exam.60] Problem 4. 計算分數(C++)

Problem 4. 計算分數
(Time Limit: 2 second)
問題描述 :
給定兩個分數 a b ,將 a +b 的結果同樣以分數呈現,且請將 a +b 結果化為最簡分數。例如 1/2+1/3 ,你要輸出 5/6

輸入說明:

輸入的第一行為一個正整數 S(1≤S≤10),代表共有幾筆測資。
每筆測資包含 2 個分數 a b ,以逗點隔開。每個分數都是以 x/y 的格式表示,其中 x y 都是整數。

輸出說明:

對於每筆測資,請輸出 a +b 的結果,並以分數表示,且分數要化為最簡分數。

範例:



Sample Input:
Sample Output:
2
1/2,1/3
1/2,1/6
5/6
2/3

說明:

依題目命名變數,依範例的測資為例來解釋題目
s = 2,代表共有幾筆測資,底下的程式碼是命名n
第一筆測資
a = 1/2,b = 1/3要用getline()讀取資料,然後有"/"和","的切割
會得到1 2 1 3
1,代表a的分子,以下程式碼用int_t[0]
2,代表a的分母,以下程式碼用int_t[1]
1,代表b的分子,以下程式碼用int_t[2]
3,代表b的分母,以下程式碼用int_t[3]
如下解釋
int_t[0]   int_t[2]
-------- + -------- =a+b
int_t[1]   int_t[3]
第一步:不管分母一不一樣,先通分
我這是先把a同乘b的分母
int_t[0]*int_t[3]   int_t[2]
------------------- + -------- =a+b
int_t[1]*int_t[3]   int_t[3]
再把b同乘a的分母
int_t[0]*int_t[3]   int_t[2]*int_t[1]
------------------- + ------------------- =a+b
int_t[1]*int_t[3]   int_t[3]*int_t[1]
假設mol和dem為相加結果的分子和分母
mol = int_t[0]*int_t[3] + int_t[2]*int_t[1]     //相加結果的分子
dem = int_t[1]*int_t[3]     //相加結果的分母

因為我在通分時,不是找最小公因素,所以要把分數化成最簡分數

  1. #include <iostream>  
  2. #include <string>  
  3. #include <sstream>  
  4. using namespace std;  
  5.    
  6. int main() {  
  7.     // Problem 4. 計算分數  
  8.     int n;  
  9.     cin >> n;  
  10.     cin.ignore();   
  11.     for(int i = 0;i < n;i++)  
  12.     {  
  13.         string input;  
  14.         cin >> input;  
  15.         //字串切割  
  16.         stringstream ss(input);  
  17.         string token, tok;  
  18.         int int_t[4];  
  19.         int c = 0;  
  20.         while (getline(ss, token, '/'))  
  21.         {  
  22.             stringstream ssA(token);  
  23.             while (getline(ssA, tok, ','))  
  24.             {  
  25.                 int_t[c] = stoi(tok);  
  26.                 c++;  
  27.             }  
  28.         }  
  29.         c = 0;  
  30.         //int_t[0] -- 數字a的分子  
  31.         //int_t[1] -- 數字a的分母  
  32.         //int_t[2] -- 數字b的分子  
  33.         //int_t[3] -- 數字b的分母 
  34.  
  35.         //int_t[0]   int_t[2]  
  36.         //-------- + -------- =a+b  
  37.         //int_t[1]   int_t[3]  
  38.         int mol;//分子  
  39.         int dem;//分母  
  40.    
  41.         int_t[0] = int_t[0]*int_t[3];  
  42.         int_t[2] = int_t[2]*int_t[1];  
  43.         mol = int_t[0] + int_t[2];  
  44.         //cout << int_t[0] << " " << int_t[2] << endl;  
  45.         int_t[1] = int_t[3] = int_t[1]*int_t[3];  
  46.         dem = int_t[1];  
  47.         //cout << int_t[1] << " " << int_t[3] << endl;  
  48.    
  49.         if(mol < dem)//分子小  
  50.         {  
  51.             for(int i = 1;i < mol;i++)  
  52.             {  
  53.                 if(mol % i == 0 && dem % i == 0)  
  54.                 {  
  55.                     if(i != 1)  
  56.                     {  
  57.                         while(mol % i == 0 && dem % i == 0)  
  58.                         {  
  59.                             mol = mol / i;  
  60.                             dem = dem / i;  
  61.                         }  
  62.                     }  
  63.                 }  
  64.             }  
  65.         }  
  66.         else  
  67.         {  
  68.             for(int i = 1;i < dem;i++)  
  69.             {  
  70.                 if(mol % i == 0 && dem % i == 0)  
  71.                 {  
  72.                     if(i != 1)  
  73.                     {  
  74.                         while(mol % i == 0 && dem % i == 0)  
  75.                         {  
  76.                             mol = mol / i;  
  77.                             dem = dem / i;  
  78.                         }  
  79.                     }  
  80.                 }  
  81.             }  
  82.         }  
  83.         cout << mol << "/" << dem << endl;  
  84.     }  
  85.     return 0;  
  86. }  

沒有留言:

張貼留言