PAT甲级1005水题飘过

题目分析:用一个字符串输入之后遍历每一位求和后,不断%10获取最后一位存储下来,逆序用对应的英文单词输出(注意输入为0的情况)

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 string a[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
 6 int b[10];
 7 
 8 int main(){
 9     string s;
10     while(cin>>s){
11         int len = s.size();
12         int sum = 0;
13         for(int i = 0; i < len; i++){
14             int x = s[i] - '0';
15             sum += x;
16         }
17 //        cout<<sum<<endl;
18         int cnt = 0;
19         if(sum == 0){
20             b[cnt++] = 0;
21         }else{
22             while(sum != 0){
23                 int x = sum % 10;
24                 sum /= 10;
25                 b[cnt++] = x;
26             }
27         }
28         for(int i = cnt-1; i >= 0; i--){
29             if(i != cnt-1) printf(" ");
30             cout<<a[b[i]];
31         }
32         printf("\n");
33     }
34     return 0;
35 }

 

上一篇:P1629 邮递员送信


下一篇:GUTC 2019/9/22题目