前几天去东莞比赛,赶上台风。
http://p.weather.com.cn/2017/08/2763670.shtml#p=1
晚上坐飞机回学校,穿过云层之后看到电闪雷鸣,大概是受影响产生的恶劣天气。
广东的同学又要躲在寝室里瑟瑟发抖了。。
题目描述
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>
#include<string>
using namespace std;
string number[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
string wei[4]={"Qian","Bai","Shi",""};
int main()
{
string re;
int flag = 0,i;
string a;
cin>>a;
if(a[0] == '-')
{
re += "Fu ";
a.erase(0,1);
}
if(a.size() == 9)
{
re += number[ (int)(a[0]-'0') ];
re += " Yi ";
a.erase(0,1);
}
int len = a.size();
int m;//记录位数
m = (len % 4); //初始化位数
int lingc = 0;//记录0的次数
if(m % 2 == 1)
{
m = 4 -m;
}
for(i=0;i<len;i++)
{
if(a[i] =='0' && len == 1)
{
re += "ling ";
}
else if((int)(a[i]-'0') != 0)
{
if(lingc == 4)
{
re += "ling ";
lingc = 0;
}
re += number[ (int)(a[i]-'0') ];
re += " ";
re += wei[m++];
if(m!=4)
re += " ";
}
else if( a[i+1]>='1' && a[i+1]<='9' && m<3)
{
re += "ling ";
m++;
}
else
{
m++;
lingc++;
}
if(m == 4 && len>4 && i<4)
{
if(lingc !=4)
re += "Wan ";
m = 0;
}
}
len = re.size();
re.erase(len-1,1);
cout<<re<<endl;
return 0;
}
这题的case比较多。
Tips:
- 把9位数(string)分成1-4-4进行处理
- 若最高位不为0,每个4位的读法是相同的
- 第一个4位若不为0,最后要加上“万”
- 注意最中输出的格式,需要擦掉一个space