1082 Read Number in Chinese (25分)

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

题目描述:将数字转化为字母拼音的格式


输入:不超过9 digits的数

输出:汉字拼音格式


解题思路:把几个坑说下,测试点3是输入0的时候是否输出'ling'。难点是对零的处理,给几个测试数据吧,测试通过基本就行了;这里提供两个写法,细节处理有些许不同。

9090909
jiu Bai ling jiu Wan ling jiu Bai ling jiu

100000009
yi Yi Wan ling jiu

0
ling

10086
yi Wan ling ba Shi liu

110110
yi Shi yi Wan ling yi Bai yi Shi

#include<bits/stdc++.h>
using namespace std;
//方法一
int main()
{
	string s;
	cin >> s;
	map<int,string> p;
	p[0]="ling"; p[1]="yi"; p[2]="er"; p[3]="san"; p[4]="si"; p[5]="wu";
	p[6]="liu"; p[7]="qi"; p[8]="ba"; p[9]="jiu";
	if(s[0]=='-'){
		printf("Fu ");
	}
	else s = "+"+s;
	string ans="";
	bool flag = false;
	int l = s.size();
	for(int i=1; i<l; i++){
		if(s[i]=='0'){
			if(l-i==5) ans += " Wan"; 
			flag = true;
			continue;
		}
		else{
			if(i!=1) ans += " ";
			if(flag){
				ans+= p[0] + " ";
			}
			flag = false;
			if(l-i==1) ans += p[s[i]-'0'];
			if(l-i==2||l-i==6) ans += p[s[i]-'0'] + " Shi";
			if(l-i==3||l-i==7) ans += p[s[i]-'0'] + " Bai";
			if(l-i==4||l-i==8) ans += p[s[i]-'0'] + " Qian";
			if(l-i==5) ans += p[s[i]-'0'] + " Wan";
			if(l-i==9) ans += p[s[i]-'0'] + " Yi";
		}
	}
	if(s=="+0"||s=="-0") ans = "ling";
	cout << ans << endl;
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
//方法二
int main()
{
	map<int,string> p;
	p[0] = "ling"; p[1] = "yi"; p[2] = "er"; p[3] = "san";
	p[4] = "si"; p[5] = "wu"; p[6] = "liu"; p[7] = "qi";
	p[8] = "ba"; p[9] = "jiu";
	string str, ans="";
	cin >> str;
	int l, s=0;
	bool flag = false;
	l = str.length();
	if(str[0]=='-'){
		ans += "Fu";
		s++; l--; flag = true;
	}
	bool f1=false, f2=false, f3=false;
	for(int i=s; i<str.length(); i++,l--){
		f1 = false;
		if(l>=5&&l<=8) f2 = true;
		if(str[i]=='0') f3 = true;
		else{
			f1 = true;
			if(flag) ans += " ";
			if(f3) ans += "ling ";
			ans += p[str[i]-'0'];
			flag = true;
			f3 = false;
		}
		if(f1){
			if(l==2||l==6) ans += " Shi";
			if(l==3||l==7) ans += " Bai";
			if(l==4||l==8) ans += " Qian";
			if(l==9) ans += " Yi";
		}
		if(f2&&l==5){
			ans += " Wan"; f2 = false;
		}
	}
	if(str=="0") ans = "ling";
	cout << ans << endl;
	return 0;
}