【PAT甲】1005 Spell It Right (20分),数字转字符串函数 s = to_string(sum)
【摘要】
problem
1005 Spell It Right (20分) Given a non-negative integer N, your task is to compute the sum of ...
problem
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 (≤10
100
).
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各位数的和,并输出英文读音
solution
- 求出和sum后,可以用函数将sum转换为string并直接输出读音
- 也可以递归到底逐位输出读音。
#include<iostream>
#include<string>
using namespace std;
int main(){
string s; cin>>s;
int sum = 0;
for(int i = 0; i < s.size(); i++)
sum += s[i]-'0';
string ans = to_string(sum);
string arr[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
cout<<arr[ans[0]-'0'];
for(int i = 1; i < ans.size(); i++)
cout<<" "<<arr[ans[i]-'0'];
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。
原文链接:gwj1314.blog.csdn.net/article/details/108410854
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)