PAT甲级 1005 Spell It Right

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

本题很简单,直接上代码:

#include<iostream>
#include<string>
using namespace std;


int main(){
	char num[100];
	int sum=0;
	cin>>num;
	int i=0;
	while(num[i] != '\0'){
		sum += (num[i] - '0');
		i++;
	}
	
	string str = to_string(sum);
	if(str[0] == '0')	cout<<"zero";
		else if(str[0] == '1')	cout<<"one";
		else if(str[0] == '2')	cout<<"two";
		else if(str[0] == '3')	cout<<"three";
		else if(str[0] == '4')	cout<<"four";
		else if(str[0] == '5')	cout<<"five";
		else if(str[0] == '6')	cout<<"six";
		else if(str[0] == '7')	cout<<"seven";
		else if(str[0] == '8')	cout<<"eight";
		else if(str[0] == '9')	cout<<"nine";
	
	for(int i=1;i<str.size();i++){
		if(str[i] == '0')	cout<<" zero";
		else if(str[i] == '1')	cout<<" one";
		else if(str[i] == '2')	cout<<" two";
		else if(str[i] == '3')	cout<<" three";
		else if(str[i] == '4')	cout<<" four";
		else if(str[i] == '5')	cout<<" five";
		else if(str[i] == '6')	cout<<" six";
		else if(str[i] == '7')	cout<<" seven";
		else if(str[i] == '8')	cout<<" eight";
		else if(str[i] == '9')	cout<<" nine";
	}
		
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_29762941/article/details/80957159
今日推荐