Stay determined.

1005 Spell It Right (20)

Posted on By Yang Guang
Viewed   times

题目

=================================================
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

思路

=================================================

字符串处理和数位处理,看代码吧

完整代码

=================================================

#include <iostream>
#include <cstring>


using namespace std;
void deal(int sum, string read[]){
	int sum_2[5];
	int sum_2len = 0;
	if(sum == 0){
		cout << "zero";
		return;
	}
	while(sum){
		sum_2[sum_2len++] = sum % 10;
		sum /= 10;
	}
	for(int i = sum_2len - 1; i > 0; i--){
		cout << read[sum_2[i]] << " ";
	}
	cout << read[sum_2[0]];
}
int main() {
	string str;
	int sum = 0;
	string read[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
	cin >> str;
	for(int i = 0; i < str.size(); i++){
		sum += str[i] - '0';
	}
	deal(sum, read);
	return 0;
}



================================================================================================