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 <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <iostream>
using namespace std;
char smallUnit[][5] = {"Shi", "Bai", "Qian"};
char bigUnit[][5] = {"Wan", "Yi"};
char num[][5] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
int main() {
string str;
queue<string> result;
cin >> str;
// 处理负号
if(str[0] == '-'){
result.push("Fu");
str.erase(0,1);
}
// 处理前导零
while (str.length() > 0 && str[0] == '0') {
str.erase(0,1);
}
// 输出全是零的情况
if(str.length() == 0){
cout << "ling";
return 0;
}
int len = (int)str.length();
// 每四位一组,输出组数
for(int group = len / 4; group >= 0; group--){
// 两个标记 有没有零和是否输出了数字
bool zero = false, hasPrint = false;
char n;
int t;
for(int j = max(0, len - (group * 4 + 3) - 1), l = len - group * 4; j < l; j++){
n = str[j] - '0';
if(n == 0){
zero = true;
}else{
hasPrint = true;
if(zero){
result.push("ling");
zero = false;
}
result.push(num[n]);
t = l - j - 2;
if(t >= 0){
result.push(smallUnit[t]);
}
}
}
// 如果该组不是第一组(第一组没有单位)且该组中输出了数字(比如100001000中第二组的“万”不会被输出)
if(group > 0 && hasPrint){
result.push(bigUnit[group - 1]);
}
}
// 输出第一个拼音(没有前导空格)
if(!result.empty()){
cout << result.front();
result.pop();
}
// 输出后面的拼音(带前导空格)
while(!result.empty()){
cout << " " <<result.front();
result.pop();
}
return 0;
}