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
注意:
1、建立两个string类的hash表,一个是数值的拼音,一个是位的拼音,根据数值和位输出。
2、这里需要特别注意的是中间0的输出,特别是万位,如果万位为0,当十万、百万、千万位有值不为0的时候,这里是需要输出“Wan”的,如题中的100800,而如果万位前的这三位都等于0,则不输出,如100000001(yi Yi ling yi),而对于非万位,如果当前值为0且下一位的值非0,则需要输出“ling”。下面代码对0的处理在这里注释以下
     1)如果当前位非0,则正常输出数值和位;
     2)如果当前位为0,则看当前位是否是万位
          i)若是万位,则看十万位、百万位、千万位是否有值不为0,有则输出"Wan",注意当输入数值的总位数小于9大于6也就是值小于1亿大于10万的时候,这三位上面肯定是有值不为0
          ii)若不是万位,但是如果下一位非0,那么这里需要输出"ling"
     3)over~~~
3、注意如果输入为0的情况,这时候输出ling,case 3就考察这种情况。

代码:
//1082
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string digit[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
	string bit[10]={"","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi"};
	string a;
	cin>>a;
	int i;
	for(i=0;i<a.size()-1;++i)
	{
		if(a[i]=='-')
			cout<<"Fu";
		else if(a[i]!='0')
		{//print the digit and the bit of this digit
			if(i) cout<<' ';
			cout<<digit[a[i]-'0']<<' '<<bit[a.size()-1-i];
		}
		else if(a[i]=='0')
		{//pay attention to this situation
			if(a.size()-i==5)
			{//sorry, my English is so poor that I don't know how to discribe here
				if(a.size()<9 || a[i-1]+a[i-2]+a[i-3]>3*'0')
					cout<<" Wan";
			}
			else if(a[i+1]!='0')//when next digit is not 0,printf "ling"
				cout<<" ling";
		}
	}
	if(a[i]!='0')
		cout<<' '<<digit[a[i]-'0']<<endl;
	else if(a[i]=='0'&&a.size()==1)//case 3: input 0
		cout<<"ling"<<endl;
	return 0;
}


你可能感兴趣的:(考试,pat,浙江大学)