当前位置:网站首页>1005 Spell It Right(20 分)(PAT甲级)

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

2022-07-04 17:58:00 相思明月楼

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;
}

 

原网站

版权声明
本文为[相思明月楼]所创,转载请带上原文链接,感谢
https://dreams.blog.csdn.net/article/details/82629765