1. 程式人生 > >pat1082 Read Number in Chinese

pat1082 Read Number in Chinese

#include<algorithm>
#include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cstring>
#define N 20005
using namespace std;
string nb[10] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
string he[3] = {"Qian","Bai","Shi"};
string ans[1000];
int main()
{
	string s;
	while(cin>>s)
	{
		int p = 0,neg = 0;
		if(s[0] == '-')
		{
			neg = 1;
			s = s.substr(1,s.length()-1);
		}
		if(s == "0")
		{
			printf("ling\n");
			continue;
		}
		while(s.length()<9)s = "0"+s;
		if(s[0]!='0')
		{
			ans[p++] = nb[s[0]-'0'];
			ans[p++] = "Yi";
		}

		int f = 0;
		for(int i = 1;i<5;i++)
		{
			if(s[i]!='0')
			{
				f = 1;
				ans[p++] = nb[s[i]-'0'];
				if(i!=4)ans[p++] = he[i-1];
			}
			else 
			{
				while(p>0&&ans[p-1] == "ling")p--;
				ans[p++] = nb[0];
			}
		}
		if(f)
		{
			while(p>0&&ans[p-1] == "ling")p--;
			ans[p++] = "Wan";
		}
		f = 0;
		for(int i = 5;i<9;i++)
		{
			if(s[i]!='0')
			{
				ans[p++] = nb[s[i]-'0'];
				if(i!=8)ans[p++] = he[i-5];
			}
			else 
			{
				while(p>0&&ans[p-1] == "ling")p--;
				ans[p++] = nb[0];
			}
		}
		int i = 0;
		while(ans[i] == "ling")i++;
		while(p>0&&ans[p-1] == "ling")p--;
		if(neg)printf("Fu ");
		for(;i<p;i++)
		{
			cout<<ans[i];
			if(i!=p-1)cout<<" ";
		}
		cout<<endl;
	}
    return 0;
}