PAT - 甲级 - 1005. Spell It Right (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 (<= 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

输入可以很大,所以不能用int,double,long,用数组保存即可,其他很简单

#include<iostream>
#include<string>
#include<vector>
using namespace std;
void Out(char s)
{
	switch (s)
	{
	case '0': {cout << "zero"; break; }
	case '1': {cout << "one"; break; }
	case '2': {cout << "two"; break; }
	case '3': {cout << "three"; break; }
	case '4': {cout << "four"; break; }
	case '5': {cout << "five"; break; }
	case '6': {cout << "six"; break; }
	case '7': {cout << "seven"; break; }
	case '8': {cout << "eight"; break; }
	case '9': {cout << "nine"; break; }
	}
}
int main()
{
	string s;
	int sum = 0;
	cin >> s;
	for (int i = 0; i < (signed)s.length(); i++)
	{
		sum += s[i] - '0';
	}
	string res = to_string(sum);
	for (int i = 0; i < (signed)res.length()-1; i++)
	{
		Out(res[i]);
		cout << " ";
	}
	Out(res[res.length()-1]);
}




扫描二维码关注公众号,回复: 2067557 查看本文章

猜你喜欢

转载自blog.csdn.net/woshikf001/article/details/80984266