题目描述
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".
输入描述:
Each input file contains one test case, which gives an integer with no more than 9 digits.
输出描述:
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.
输入例子:
-123456789
输出例子:
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
#include<iostream>
using namespace std;
int main(){
string f[10] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
int x;
int flag = 0; //标记是否需要空格,0为不需要空格
int wan = 0; //标记是否有万位
int zero = 0; //标记之前所有位是否大于0,0表示为0,1表示之前某位有大于0的
int pre = 0; //标记该数的前一位是否为0,0表示为0,1表示不为0
int qian[2] = {0}; //标记千位前是否需要ling
cin >> x;
if (x < 0) {
cout << "Fu";
flag = 1; //有负号,则改为1,说明需要空格
x = abs(x);
}
if (x == 0)
cout << "ling";
if (x / 100000000 != 0) {
if (flag == 1)
cout << " ";
cout << f[x/100000000] << " Yi";
x = x % 100000000;
flag = 1;
pre = 1;
}
else
pre = 0;
//---------------------------------------------
if (pre == 1) { //看亿位是否存在,用于百位的判断
zero = 1;
qian[0] = 1;
}
else
zero = 0;
if (x / 10000000 != 0) {
if (flag == 1)
cout << " ";
cout << f[x/10000000] << " Qian";
x = x % 10000000;
flag = 1;
wan = 1;
pre = 1;
qian[1] = 1;
}
else
pre = 0;
//-------------------------------------------
if (x / 1000000 != 0) {
if (flag == 1)
cout << " ";
if (pre == 0 && zero == 1)
cout << "ling ";
cout << f[x/1000000] << " Bai";
x = x % 1000000;
flag = 1;
wan = 1;
pre = 1;
qian[1] = 1;
}
else {
if (zero != 1 && pre == 1) {
zero = 1;
}
pre = 0;
}
//-------------------------------------------
if (x / 100000 !=0) {
if (flag == 1)
cout << " ";
if (pre == 0 && zero == 1)
cout << "ling ";
cout << f[x/100000] << " Shi";
x = x % 100000;
flag = 1;
wan = 1;
pre = 1;
qian[1] = 1;
}
else {
if (zero != 1 && pre == 1) {
zero = 1;
}
pre = 0;
}
//-------------------------------------------
if (x / 10000 != 0) {
if (flag == 1)
cout << " ";
if (pre == 0 && zero == 1)
cout << "ling ";
cout << f[x/10000] ;
x = x % 10000;
flag = 1;
wan = 1;
pre = 1;
qian[1] = 1;
}
else {
if (zero != 1 && pre == 1) {
zero = 1;
}
pre = 0;
}
//-------------------------------------------
if (wan == 1)
cout << " Wan";
//-------------------------------------------
if (x / 1000 != 0) {
if (flag == 1)
cout << " ";
if (qian[0] == 1 && qian[1] == 0)
cout << "ling ";
cout << f[x/1000] << " Qian";
x = x % 1000;
flag = 1;
pre = 1;
}
else {
if (zero != 1 && pre == 1) {
zero = 1;
}
pre = 0;
}
//-------------------------------------------
if (x / 100 != 0){
if (flag == 1)
cout << " ";
if (pre == 0 && zero == 1)
cout << "ling ";
cout << f[x/100] << " Bai";
x = x % 100;
flag = 1;
pre = 1;
}
else {
if (zero != 1 && pre == 1) {
zero = 1;
}
pre = 0;
}
//-------------------------------------------
if (x / 10 != 0) {
if (flag == 1)
cout << " ";
if (pre == 0 && zero == 1)
cout << "ling ";
cout << f[x/10] << " Shi";
x = x % 10;
flag = 1;
pre = 1;
}
else {
if (zero != 1 && pre == 1) {
zero = 1;
}
pre = 0;
}
//-------------------------------------------
if (x != 0) {
if (flag == 1)
cout << " ";
if (pre == 0 && zero == 1)
cout << "ling ";
cout << f[x];
}
return 0;
}