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

版权声明:版权所有 https://blog.csdn.net/Adusts/article/details/82629765

Problem Description:

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 (≤10​100​​).

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

一道简单题,注意一下为0的情况

#include <iostream>
#include <cstring>
using namespace std;
int main() {
	char s[110];
	string num[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
	scanf("%s", s);
	int sum = 0, len = strlen(s);
	for(int i = 0; i < len; i++) {
		sum += (s[i] - '0');
	}
	int t, k = 0;
	string ans[110];
	if(sum == 0) {
		printf("zero\n");
	} else {
		while(sum) {
			t = sum%10;
			ans[k++] = num[t];
			sum /= 10;
		}
		for(int i = k-1; i > 0; i--) {
			cout<<ans[i]<<" ";
		}
		cout<<ans[0]<<endl;		
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Adusts/article/details/82629765