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 原题链接: https://www.patest.cn/contests/pat-a-practise/1082https://www.nowcoder.com/pat/1/problem/4312
思路:
分解,判断,输出
CODE:
#include<iostream> #include<cstring> #include<string> #include<map> #include<cstdio> #include<vector> using namespace std; string s[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; vector<string> op; int main() { int n; cin>>n; if (n<0) { n*=-1; if (n!=0) op.push_back("Fu"); } int a=n/100000000; int b=n0000000/10000; int c=n000; //cout<<a<<" "<<b<<" "<<c<<endl; if (a!=0) { op.push_back(s[a]); op.push_back("Yi"); } if (b!=0) { int b1=b/1000,b2=b00/100,b3=b0/10,b4=b; if (b1!=0) { op.push_back(s[b1]); op.push_back("Qian"); } else { if (a!=0) op.push_back("ling"); } if (b2!=0) { op.push_back(s[b2]); op.push_back("Bai"); } else { if (b1!=0&&!(b3==0&&b4==0)) op.push_back("ling"); } if (b3!=0) { op.push_back(s[b3]); op.push_back("Shi"); } else { if (b2!=0&&b4!=0) op.push_back("ling"); } if (b4!=0) op.push_back(s[b4]); op.push_back("Wan"); } else { if (a!=0&&c!=0) op.push_back("ling"); } if (c!=0) { int c1=c/1000,c2=c00/100,c3=c0/10,c4=c; if (c1!=0) { op.push_back(s[c1]); op.push_back("Qian"); } else { if (!(b==0)) op.push_back("ling"); } if (c2!=0) { op.push_back(s[c2]); op.push_back("Bai"); } else { if (c1!=0&&!(c3==0&&c4==0)) op.push_back("ling"); } if (c3!=0) { op.push_back(s[c3]); op.push_back("Shi"); } else { if (c2!=0&&c4!=0) op.push_back("ling"); } if (c4!=0) op.push_back(s[c4]); } if (a==0&&b==0&&c==0) { cout<<"ling"; } else { cout<<op[0]; for (int i=1;i<op.size();i++) { cout<<" "<<op[i]; } } return 0; }