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

题目大意:给出一个非负整数N(N<=10^100),将每一位相加,用英文输出各位和.

(简单题)由于N过大故需要用数组来保存,可以以字符串形式输入,逐位相加求出sum,再将sum逆序保存在另一个数组中,从最高位(注意数组中保存着sum的逆序)开始查询相应的英文单词

#include<iostream>
#include<string.h>
using namespace std;
int main(){
	char num[10][10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
	int index[1000],biao=0;
	char str[1000];
	int sum=0;
    while(scanf("%s",&str)!=EOF){
    	int len=strlen(str);
    	sum=0;
    	biao=0;
		for(int i=0;i<len;i++){
			sum+=(str[i]-'0');
		}
		if(sum==0){
			cout<<"zero"<<endl;;
			continue;
		}
		while(sum){//将sum中的数字逆序保存在index数组中 
			int digit=sum%10;
			index[biao]=digit;
			biao++;
			sum/=10;
		}
		for(int i=biao-1;i>=0;i--){
			if(i==biao-1)cout<<num[index[i]];
			else{
				cout<<" "<<num[index[i]];
			}
		}
		cout<<endl;
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yx970326/article/details/80201567