【PAT甲级】1005 Spell It Right (20 分)

Spell It Right

题目描述

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (≤10100​ ).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:

12345

Sample Output:

one five

思路

本题比较简单,注意不能用数字类型储存输入,因为最大数字范围在10100,因此可以使用一个长度为102的字符串来保存数字

由于最大的数字也不过100位,即使每一位都是9,加起来和也不过1000,因此输出英文的时候,可以用三个整数来分别存储个位,十位,百位,然后根据情况输出即可

代码

注:可以用一个字符串数组来记录0-9的英文单词

#include
#include

char *number[10]={"zero","one","two","three","four",
						"five","six","seven","eight","nine"};

int main()
{
	char s[102];
	scanf("%s",&s);
	int sum=0;
	for(int i=0;i<strlen(s);i++)//include string.h head document to use function strlen
	{
		sum+=s[i]-'0';
	}
	int n1,n2,n3;
	n3=sum%10;sum/=10;
	n2=sum%10;sum/=10;
	n1=sum%10;
	if(n1!=0)
	{
		printf("%s %s %s\n",number[n1],number[n2],number[n3]);
	}
	else if(n2!=0)
	{
		printf("%s %s\n",number[n2],number[n3]);
	}
	else
	{
		printf("%s\n",number[n3]);
	}
	return 0;
}

git仓库:Spell It Right

你可能感兴趣的:(PAT考试)