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 <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
string s;
void zerocheck(int pos)
{
}
int main()
{
//freopen("in.txt", "r", stdin);
string ntoc[] = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" },
digit[] = { "Qian","", "Shi", "Bai", };//注意这里的排序,是按照relative_pos的关系来排的。
cin >> s;
if (s=="0")//单独考虑s为0的情况
{
cout << "ling";
return 0;
}
if (s[0] == '-')
{
printf("Fu ");
s = s.substr(1);//把符号舍去,只处理数字
}
int len = s.length();
// reverse(s.begin(), s.end());
vector<string> res[9];
for (int i = 0; i < len; i++)
{
int t = s[i] - '0';
int relative_pos = (len - i) % 4; //四个为一组,仅0所在组而言,0后面有非0数字,就念“ling”
if (t == 0) //对0进行处理,是否念“ling”
{
//if (s[i - 1] == '0' && (relative_pos == 0)) //0前面还有0的情况
//{
// res[i].push_back("none"); 不需要考虑之前是否有0的情况,只看后面就行了
//}
//四个为一组,仅0所在组而言,0后面有非0数字,就念“ling”
if (relative_pos==0)//在一组中,0处于最高位的情况,但是relative_pos是0,下面验证后面是否全零的时候:j<relative_pos失效了,所以直接赋值为4。
{
relative_pos = 4;
}
bool allzero = true;//全零的flag
for (int j = 1; j < relative_pos; j++)
{
if ((s[i + j] - '0') != 0)
{
allzero = false;
break;
}
}
if (allzero)//none代表无效零,这种零不用管
{
res[i].push_back("none");
}
}
//0的情况全部处理完了,剩下就算遇到0也按正常处理,也要念ling
res[i].push_back(ntoc[t]); //数字念法
res[i].push_back(digit[relative_pos % 4]);//位数念法
}
//output
for (int i = 0; i < len; i++)
{
for (int j = 0; j < res[i].size(); j++)
{
if (res[i][j] != "none"&&res[i][j]!="")//不然res[i][j]==""的时候也会输出一次空格,导致格式错误
{
if (i != 0 || j != 0)//注意格式
{
cout << " ";
}
cout <<res[i][j];
if (res[i][j] == "ling")
{
break;
}
}
else
{
break;
}
}
if ((len - i) / 4 == 2 && (len - i) % 4 == 1)
{
cout << " Yi";
}
else if ((len - i) / 4 == 1 && (len - i) % 4 == 1)
{
cout << " Wan";
}
}
return 0;
}