1005. Spell It Right (20)

2017/10/13 PAT 共 904 字,约 3 分钟

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

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main() {
#ifdef LOCAL
	freopen("d:\\datain.txt", "r", stdin);
#endif LOCAL

	char s[101];
	scanf("%s", s);
	int n = strlen(s);
	long long ans=0;
	for (int i = 0; i < n; i++) {
		ans = ans + s[i] - '0';
	}
	char* str[10] = { "zero","one","two","three","four","five","six","seven","eight","nine" };
	char s1[101];
	sprintf(s1, "%d", ans);
	int n1 = strlen(s1);

	printf("%s", str[s1[0]-'0']);
	for (int i = 1; i < n1; i++) {
		printf(" %s", str[s1[i]-'0']);
	}
	return 0;
}

Search

    Table of Contents