PAT1082. Read Number in Chinese (25)

来源:互联网 发布:windows update更新慢 编辑:程序博客网 时间:2024/05/21 01:28

http://pat.zju.edu.cn/contests/pat-a-practise/1082

Given 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.

Sample Input 1:
-123456789
Sample Output 1:
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
Sample Input 2:
100800
Sample Output 2:
yi Shi Wan ling ba Bai
#include <string>#include <vector>#include <iostream>using namespace std;int main(){string a[] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};string b[] = {"", "Shi", "Bai", "Qian", "Wan", "Shi", "Bai", "Qian", "Yi"};vector<string> res;vector<int> digit;int num, e;cin >> num;if (num == 0){cout << "ling";return 0;} else if (num < 0){cout << "Fu ";num = -num;}while (num != 0){digit.push_back(num%10);num /= 10;}for (e=0; e<digit.size() && digit[e]==0; ++ e) {}if (e == 8){cout << a[digit[e]] << " Yi";return 0;}for (int i = e; i < digit.size(); ++ i){if (i!=0 && (digit[i]!=0 || i==4 || i==8)){res.push_back( b[i] );}res.push_back(a[ digit[i] ]);}for (int i = res.size()-1; i >= 0; -- i){if (i != res.size()-1){cout << " ";}int cnt = 0;while (i>=0 && res[i]=="ling"){-- i;++ cnt;}if (cnt!=0 && res[i]!="Wan"){cout << "ling ";}cout << res[i];}return 0;}


0 0
原创粉丝点击