题目
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 Input1:
-123456789
Sample Output1:
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
Sample Input2:
100800
Sample Output2:
yi Shi Wan ling ba Bai
Note
“边读边输出”。
变量解释:
- len:数字的位数(若是负数则等于字符串的长度-1)
len≤0时,break直接退出while - is0:判断0的存在。这里is0有三个状态
1、is0=0:判断0在头部,即0在非零数字之前。当is0等于0时,第一个非零数字之前的0不用读。(如:0001,应该读作yi)
2、is0=1:判断0在中部。这时需要判断0是否包括了万这个位。
例如:100001,读作yi Shi Wan ling yi;
110001,读作yi Shi yi Wan ling yi。
若0跨越了万位,需要输出Wan;反之不需要输出。
3、is0=2:判断末尾是否有0。若0在末尾,不需要额外输出0。(例如:100,读作yi Bai)
特殊的测试点?
100000
000000
100001
110001
Code:
#include<bits/stdc++.h>
using namespace std;
int main(){
string n;
string number[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
string weight[10]={"0","1","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi"};
cin>>n;
int i=0,len=n.length(),is0=0,flag=0;
if(n[i]=='-'){
len--;
i++;
printf("Fu");
flag=1;
}
while(len>0){
while(is0==0&&n[i]=='0'){
//第一个非零数前面的0不需要读
len--;
i++;
}
if(is0==0&&n[i]!='0') is0=1;
if(len<=0){
//筛选0和0000这种情况
printf("ling");
break;
}
while(is0==1&&n[i]!='0'&&len>0){
if(flag==1) cout<<" ";
else if(flag==0) flag=1;
//先输出数字 再输出单位
cout<<number[n[i]-'0'];
if(len>1) cout<<" "<<weight[len];
i++;
len--;
}
if(len<=0) break; //len<=0 直接退出
int before0=n.length();
if(is0==1&&n[i]=='0'){
is0=2;
before0=len; //记录中间部分第一个0的位置
}
while(is0==2&&n[i]=='0'){
len--;
i++;
}
if(before0>=5&&len<5) cout<<" Wan"; //0跨越了万位,需要输出Wan
if(is0==2) is0=1;
if(before0!=len&&len!=0) cout<<" ling"; //如果中间部分有0,则需要读出来
}
return 0;
}