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:-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
分析:要注意0的转换,如0, 10, 100, 108, 1008, 100008 等情况。
代码如下:
#include <iostream>
#include <vector>
#include <string>
#include <stack>
using namespace std;
void toChinese(vector<string> &v){
v.push_back("ling");
v.push_back("yi");
v.push_back("er");
v.push_back("san");
v.push_back("si");
v.push_back("wu");
v.push_back("liu");
v.push_back("qi");
v.push_back("ba");
v.push_back("jiu");
}
void setOther(vector<string> &v){
v.push_back("Shi");
v.push_back("Bai");
v.push_back("Qian");
v.push_back("Wan");
}
int main(int argc, char** argv) {
vector<string> chinese;
toChinese(chinese);
vector<string> other; //进制
setOther(other);
int i, num, cnt, t;
string str;
cin>>str;
if( (str.size()==1&&str[0]=='0') || (str.size()==2 && str[0]=='-' && str[1]=='0') ){
printf("ling\n");
return 0;
}
stack<string> sta;
cnt = 0;
t = 0;
for(i=str.size()-1; i>=0; i--){
t++;
num = str[i]-'0';
if(t==9){
//亿
sta.push("Yi");
sta.push(chinese[num]);
continue;
}
if(str[i]=='-'){
sta.push("Fu");
break;
}
if(num != 0){
if(cnt >= 1){
sta.push(other[cnt-1]);//进制
}
sta.push(chinese[num]);//数字
}else{
if(sta.empty() || sta.top()=="ling"){
if(!sta.empty() && sta.top()=="ling" && cnt==4){
sta.push("Wan");
}
}else{
if(sta.top()!="Wan")
sta.push("ling");
}
}
if(cnt == 4){
cnt=0;
}
cnt++;
}
cout<<sta.top();
sta.pop();
while(!sta.empty()){
cout<<" "<<sta.top();
sta.pop();
}
cout<<endl;
return 0;
}