PAT (Advanced Level) Practice 1005 Spell It Right (20 分) (switch)

博客要求对给定的非负整数N,计算其各位数字之和,并将和的每一位数字用英文输出。给出了输入规格、输出规格,还包含示例输入和输出,但示例内容未给出具体值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <stack>
 7 using namespace std;
 8 string s;
 9 void _print(int x)
10 {
11     stack<int> s;
12     while(x){
13         s.push(x%10);
14         x/=10; 
15     }
16     while(!s.empty()){
17         switch(s.top()){
18             case 0: cout<<"zero"; break;
19             case 1: cout<<"one";break;
20             case 2: cout<<"two";break;
21             case 3: cout<<"three";break;
22             case 4: cout<<"four";break;
23             case 5: cout<<"five";break;
24             case 6: cout<<"six";break;
25             case 7: cout<<"seven";break;
26             case 8: cout<<"eight";break;
27             case 9: cout<<"nine";break;
28         }
29         s.pop();
30         if(!s.empty()) cout<<" ";
31     }
32     cout<<endl;
33 }
34 int main()
35 {
36     while(cin>>s){
37         int sum=0;
38         for(int i=0;i<s.length();i++){
39             sum+=int(s[i]-'0');
40         }
41         if(sum==0) cout<<"zero"<<endl;
42         else _print(sum);
43     }
44     return 0;
45 }

 

转载于:https://www.cnblogs.com/shixinzei/p/11053264.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值