PAT (Advanced Level) Practice 1005 Spell It Right (20)(20 分)

1005 Spell It Right (20)(20 分)

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

题解1:

由于数据范围很大,考虑以字符串形式读入。

计算好各位数和之后,如果以数字格式输出,则必须要求和的位数,然后从高位到低位依次计算输出。

将和从数字格式转换成字符串,直接输出,能够避免繁琐的计算。

源代码1:

#include <iostream>
using namespace std;
int main()
{
	string tab[10] = { "zero","one","two","three","four","five","six","seven","eight","nine"};
	string s;
	int len,i,sum=0;
	cin >> s;
	len = s.length();
	for (i = 0; i < len; i++)
		sum += s[i] - '0';
	s = to_string(sum);
	len = s.length();
	for (i = 0; i < len; i++)
	{
		cout << tab[s[i]-'0'];
		if (i != len - 1) cout << " ";
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/yi976263092/article/details/80724958
今日推荐