pat1005. Spell It Right (20)

本文介绍了一个使用C++解决的问题:给定一个非负整数N,计算其各位数字之和,并将每个数字以英文单词形式输出。程序包括输入处理、求和逻辑、以及将数字转换为英文单词的输出方法。

1005. Spell It Right (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <string>
 4 #include <queue>
 5 #include <stack>
 6 #include <iostream>
 7 using namespace std;
 8 string digits[15]={"zero","one","two","three","four","five","six","seven","eight","nine"};
 9 int main(){
10     string ss;
11     int sum=0,i=0,csum=0;
12     cin>>ss;
13     while(i<ss.length()){
14         sum+=ss[i++]-'0';
15     }
16     if(!sum){
17         cout<<digits[sum]<<endl;
18         return 0;
19     }
20     stack<string> s;
21     while(sum){
22         s.push(digits[sum%10]);
23         sum/=10;
24     }
25     cout<<s.top();
26     s.pop();
27     while(!s.empty()){
28         cout<<" "<<s.top();
29         s.pop();
30     }
31     return 0;
32 }

 

转载于:https://www.cnblogs.com/Deribs4/p/4663470.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值