(c)Pat刷题日记-1005 Spell It Right (20 分)

最终还是决定用c来做题目好了。。。

第五题就是字符串处理
一开始用的代码很复杂(用switch case 语句)
其实根本没必要
用一个char【】保存数字就好了

忘记了getchar()的使用

数字倒叙输出可以用数组保存再倒叙输出

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char *argv[]) {
		
		int sum = 0;
		int index = 0;
		int j =0;
		int s[4];
		
		char *num[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
		char c;
		while((c=getchar())!='\n')
		{
			sum+=c-'0';
		}
		while(sum/10 != 0)
		{
			s[index++]=sum%10;
			sum/=10;
		}
		s[index]=sum;

		for(j=index;j>=0;j--)
		{
			if(j==index)
				printf("%s",num[s[j]]);
			else
				printf(" %s",num[s[j]]);		
		}
}
在这里插入代码片

猜你喜欢

转载自blog.csdn.net/qq_15556537/article/details/88079341
今日推荐