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
题目大意:用拼音的方式输出给定数字
这道题最好把数字分组,每组4个数字,依次处理。,先处理千位及以前,然后处理千万到万之间,最后处理亿。从低位到高位,将string存入vector,最后倒序输出。
下面代码有个小问题,例如输入100000000应该输出yi Yi,但是只能输出yi,因为在main函数中的循环结束后,num==0会导致下次循环直接break掉,添加一个变量看看是否改变了,改变了就在循环退出时加上个位就行了。不过下面的代码依然可以AC。
FuckFourStep函数返回的modify变量的含义是,判断处理的这四位是否全0,如果例如100000000应输出yi Yi,而不是yi Yi Wan。
如果输入的是0,必须输出ling。
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const string num_str[] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
const string q_str[] = {"", "Shi", "Bai", "Qian"};
const string step[] = {"", "Wan", "Yi"};
bool FuckFourStep(int & num, vector<string> & ans){
bool have_pre_zero = true, modify = false;
for(int i = 0; i < 4; i++){ //前四位
if(num == 0) break; //如果是0了,停止
int bit = num % 10;
if(bit != 0){ //当前位不是0
ans.push_back(q_str[i]); //记录数字
ans.push_back(num_str[bit]);//记录位
have_pre_zero = false;
modify = true;
}
else if(bit == 0){ //当前位是0
if(!have_pre_zero) ans.push_back(num_str[0]); //无前导0,记录0
have_pre_zero = true;
}
num /= 10;
}
return modify;
}
int main(){
int num;
cin >> num;
if(num < 0){ //符号
cout << "Fu ";
num = -num;
}
vector<string> ans;
bool last_modify = false;
for(int i = 0; i < 3; i++){
if(num == 0) break;
if(last_modify) ans.push_back(step[i]);
last_modify = FuckFourStep(num, ans);
}
for(int i = ans.size() - 1; i >= 0; i--){
if(ans[i] == "") continue;
if(i != ans.size() - 1) cout << ' ';
cout << ans[i];
}
if(ans.size() == 0) cout << "ling";
cout << endl;
return 0;
}