PAT 甲级 1082 Read Number in Chinese

1082 Read Number in Chinese (25 point(s))

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

经验总结:

这一题......字符串复杂起来是真的头疼= =,做不来做不来,根据参考书上的自己码了一遍,考试考到这样的估计也是直接GG了....
自己总结一下书上的方法,运用双指针的方法,右指针从右边每隔四位的向左移动直至左右指针相差小于4,说明此时左右指针间隔出来的是即将输出的,且right是这四位中个位所在的位置,关于空格的输出,只要不是首位,意思就是比如10000 其中的yi就是首位,前面不需要空格,后面的肯定都需要了,输出数本身之后,还要根据left与right之间的距离输出十百千这样的单位,isprint意思是如果这个间隔都没有打印,那么下面就不用输出四位大间隔的万或者亿了,不过如果isprint为true,那么就需要打印出来,打印方法就是根据right与len-1之间的距离为4的几倍,然后打印出相应的数阶,就这么多吧= =

AC代码

#include 
#include 

int main()
{
	int n;
	char str[15];
	scanf("%s",str);
	char num[15][6]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
	char wei[5][6]={"Shi","Bai","Qian","Wan","Yi"};
	int len=strlen(str);
	int left=0,right=len-1;
	if(str[left]=='-')
	{
		printf("Fu");
		++left;
	}
	while(left+4<=right)
		right-=4;
	while(left0&&str[left]=='0')
				flag=true;
			else
			{
				if(flag==true)
				{
					printf(" ling");
					flag=false;
				}
				if(left>0)
					printf(" ");
				printf("%s",num[str[left]-'0']);
				isprint=true;
				if(left!=right)
					printf(" %s",wei[right-left-1]);
			}
			++left;
		}
		if(isprint==true&&right!=len-1)
			printf(" %s",wei[(len-1-right)/4+2]);
		right+=4;
	}
	return 0;
}

 

你可能感兴趣的:(PAT甲级,PAT甲级/乙级机试经验)