PAT A1082.Read Number in Chinese(满分)

1082. Read Number in Chinese (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

题目: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

#include
#include

char EnglishToC[11][10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
char s[10][8]={"absent","Shi","Bai","Qian"};

char str[20];
int len=0;

bool check(int st,int ed)
{
	for(int i=st;i<=ed;i++)
		if(str[i]!='0')
			return true;
	return false;
}

bool print(int st,int ed,int num)
{
	bool sign=false;
	for(int i=st;i<=ed;i++)
	{
		int temp=len-1-i;
		if(str[i]!='0')
		{
			num++;
			sign=true;
			if(num!=1)
				printf(" ");
			printf("%s",EnglishToC[str[i]-'0']);
			if(temp%4!=0)
				printf(" %s",s[temp%4]);
		}
		else if(sign==true && i-1>=0 && str[i-1]!='0' && check(i,ed))
			printf(" ling");
	}
	
	return sign;		
}

//这里有3分; 
bool specialCheck()
{
	bool sign=false;
	
	if(len==2 && str[0]=='-' && str[1]=='0')
	{
		printf("ling");
		sign=true;
	}
	else if(len==1 && str[0]=='0')
	{
		printf("ling");
		sign=true;
	}
	
	return sign;	
}

int main()
{
	scanf("%s",str);
	
	len=strlen(str);
	
	if(specialCheck())
		return 0;
	
	if(str[0]=='-')
	{
		printf("Fu ");
		for(int i=1;i=1 && len<=4)
		print(0,len-1,0);
	else if(len>=5 && len<=8)
	{
		bool t=print(0,len-5,0);
		if(t)
			printf(" Wan");
		if(t && str[len-4]=='0' && str[len-5]=='0' && check(len-4,len-1))
			printf(" ling");
		if(check(len-4,len-1))
			printf(" ");
		print(len-4,len-1,0);	
	}
	
	else if(9==len)
	{
		if(str[0]!='0')
		{
			printf("%s ",EnglishToC[str[0]-'0']);
			printf("Yi");
		}
		if(str[0]!='0' && str[1]=='0' && check(1,4))
			printf(" ling");
			
		if(check(1,4))
			printf(" ");	
		bool t=print(1,4,0);
		
		if(t)
			printf(" Wan");
		else if(str[0]!='0' && check(5,8))
			printf(" ling");
		if(check(5,8))
			printf(" ");
		print(5,8,0);
	}
	
	return 0;
}

这个折腾了几个小时,终于满分。中间有一些细节易错。这里主要思想是分类讨论。

你可能感兴趣的:(PAT)