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


IDEA

1.string类型作为输入,可以任意长度

2.string类型与int类型的转换,#include<sstream> stringstream ss; ss<<s; ss>>a;


CODE

#include<iostream>
#include<cstring>
#include<sstream>
using namespace std;
string change(char c){
	string s;
	switch(c){
		case '0':s="zero";break;
		case '1':s="one";break;
		case '2':s="two";break;
		case '3':s="three";break;
		case '4':s="four";break;
		case '5':s="five";break;
		case '6':s="six";break;
		case '7':s="seven";break;
		case '8':s="eight";break;
		case '9':s="nine";break;
	}
	return s;
}
int main(){
	string str;
	cin>>str;
	int sum=0;
	for(int i=0;i<str.length();i++){
		sum+=(str[i]-'0');
	}
	//cout<<"sum="<<sum<<endl;
	stringstream ss;
	string result;
	ss<<sum;
	ss>>result;
	//cout<<"result="<<result<<endl;
	for(int i=0;i<result.length();i++){
		if(i==0){
			cout<<change(result[i]);
		}else{
			cout<<" "<<change(result[i]);
		}
	}
	return 0;
}


你可能感兴趣的:(pat)