2018年1月12日 星期五

[ITSA Exam.61] Problem 2. 找錢問題




  1. #include <iostream>  
  2. #include <string>   
  3. #include <sstream>  
  4. #include <stdio.h>  
  5. #include <ctype.h>  
  6. #include <algorithm>    
  7. #include <cmath>  
  8.    
  9. using namespace std;  
  10.    
  11. int main() {  
  12.     // Problem 2. 找錢問題  
  13.     string N;  
  14.     getline(cin, N);  
  15.     int n = stoi(N);  
  16.     string input;  
  17.     for(int i = 0;i < n;i++)  
  18.     {  
  19.         getline(cin, input);  
  20.         stringstream ss(input);  
  21.         string token;  
  22.         int product[3];  
  23.         int c = 0;  
  24.         while (getline(ss, token, ','))  
  25.         {  
  26.             product[c] = stoi(token);  
  27.             c++;  
  28.         }  
  29.         int change;  
  30.         if(product[1] == 1)  
  31.         {  
  32.             change = product[0] - 17*product[2];  
  33.         }  
  34.         else if(product[1] == 2)  
  35.         {  
  36.             change = product[0] - 25*product[2];  
  37.         }  
  38.    
  39.         int coin[4] = {1, 5, 10, 50};  
  40.         int chan[4];  
  41.         fill(chan, chan+4, 0);  
  42.         chan[3] = floor(change/50);  
  43.         chan[2] = floor((change%50)/10);  
  44.         chan[1] = floor((change%50%10)/5);  
  45.         chan[0] = floor((change%50%10%5)/1);  
  46.         int sp = 0;  
  47.         for(int j = 0;j < 4;j++)  
  48.         {  
  49.             if(chan[j] != 0)  
  50.             {  
  51.                 if(sp != 0)  
  52.                 {  
  53.                     cout << ",Coin " << coin[j] << ":" << chan[j];  
  54.                 }  
  55.                 else  
  56.                 {  
  57.                     cout << "Coin " << coin[j] << ":" << chan[j];  
  58.                     sp++;  
  59.                 }  
  60.             }  
  61.         }  
  62.         cout << endl;  
  63.     }  
  64.     return 0;  
  65. }  

沒有留言:

張貼留言