PTA-1005—— Spell It Right

题目:

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 (≤).

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

题目分析:

加数只有1位的大整数加法,但要考虑输入为0的特殊情况

代码:

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 int ans[110];
 5 string number[10]={"zero","one","two","three","four",
 6                     "five","six","seven","eight","nine"};
 7 string s;
 8 int main(){
 9     cin>>s;
10     memset(ans,0,sizeof(ans));
11     for(int i=0;i<s.length();i++){
12         ans[0]+=s[i]-'0';
13         for(int j=0;j<s.length();j++){
14             if(ans[j]>=10){
15                 ans[j]-=10;
16                 ans[j+1]+=1;
17             }
18         }
19     }
20     bool flag=false;
21     for(int i=s.length()-1;i>=0;i--){
22         if(ans[i]!=0||flag){
23             flag=true;
24             if(i==0){
25                 cout<<number[ans[i]];
26             }else{
27                 cout<<number[ans[i]]<<" ";
28             }
29         }
30     }
31     if(!flag){            //一个特殊情况需要考虑:当输入为0时 
32         cout<<"zero";
33     }
34     return 0;
35 } 
 

猜你喜欢

转载自www.cnblogs.com/orangecyh/p/10274454.html