前幾天去東莞比賽逗载,趕上臺(tái)風(fēng)。
http://p.weather.com.cn/2017/08/2763670.shtml#p=1
晚上坐飛機(jī)回學(xué)校,穿過云層之后看到電閃雷鳴囚聚,大概是受影響產(chǎn)生的惡劣天氣。
廣東的同學(xué)又要躲在寢室里瑟瑟發(fā)抖了标锄。顽铸。
題目描述
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;//記錄位數(shù)
m = (len % 4); //初始化位數(shù)
int lingc = 0;//記錄0的次數(shù)
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位數(shù)(string)分成1-4-4進(jìn)行處理
- 若最高位不為0料皇,每個(gè)4位的讀法是相同的
- 第一個(gè)4位若不為0谓松,最后要加上“萬”
- 注意最中輸出的格式,需要擦掉一個(gè)space