PAT1082:Read Number in Chinese
阿新 • • 發佈:2017-11-27
while key 思路 each hand digi cpp names else if
Sample Input 1:
1082. Read Number in Chinese (25)
時間限制 400 ms 內存限制 65536 kB 代碼長度限制 16000 B 判題程序 Standard 作者 CHEN, YueGiven an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output "Fu" first if it is negative. For example, -123456789 is read as "Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu". Note: zero ("ling") must be handled correctly according to the Chinese tradition. For example, 100800 is "yi Shi Wan ling ba Bai".
Input Specification:
Each input file contains one test case, which gives an integer with no more than 9 digits.
Output Specification:
For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.
-123456789Sample Output 1:
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiuSample Input 2:
100800Sample Output 2:
yi Shi Wan ling ba Bai
思路
邏輯題,有點惡心。。。
1.先輸入一個數,將單個零和負數的情況先處理掉(後續將1億的情況也單獨處理掉)。
2.將該數的每一位數字用一個int數組的形式存放。
3.遍歷該數組,根據該數組的每一位的位數和單個數字將對應的字符串插入到一個新的vector中,為0的位數除了是萬位或者億位以外都不用插入位數的字符串。
4.對於多余的零做處理,並輸出每一位的數字和位數。
代碼
#include<iostream> #include<vector> using namespace std; vector<string> n={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; vector<string> digit={"","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi"}; using namespace std; int main() { vector<string> res; vector<int> number; int num,index; cin >> num; if(num == 0) { cout << "ling" << endl; return 0; } else if( num < 0) { cout << "Fu "; num = -num; } while(num != 0) { number.push_back(num % 10); num /= 10; } for(index = 0;index < number.size() && number[index] == 0;index++); if(index == 8) { cout << n[number[index]] << " Yi"; return 0; } for(int i = index;i < number.size();i++) { if(i != 0 && (number[i] != 0 || i == 4 || i == 8)) res.push_back(digit[i]); res.push_back(n[number[i]]); } for(int i = res.size() - 1;i >= 0;i--) { if(i != res.size() - 1) cout << " "; int zerocnt = 0; while( i >= 0 && res[i] == "ling") { i--; zerocnt++; } if(zerocnt > 0 && res[i] != "Wan") { cout << "ling "; } cout << res[i]; } }
PAT1082:Read Number in Chinese