PAT (Advanced Level) 1005 Spell It Right (20 分)
发布日期:2021-06-29 12:22:20 浏览次数:2 分类:技术文章

本文共 1160 字,大约阅读时间需要 3 分钟。

序:

这道题,无他,字符串处理,建个对应的英文表就好了

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

题目概述:

给出一个长数字,把每一位的数加起来,然后按英文的0-9输出结果

分析:

1.用string存储,否则溢出
2.建立dict对应0-9的英文

#include
using namespace std;string dict[10] = {
"zero","one","two","three","four","five","six","seven","eight","nine"};int main(){
string str1; cin >> str1; int sum = 0; for(int i = 0; i < str1.size(); i++) {
sum += str1[i] - '0'; } string str2; str2 = to_string(sum); for(int i = 0; i < str2.size(); i++) {
if(i == 0) printf("%s", dict[str2[i] - '0'].c_str()); else printf(" %s", dict[str2[i] - '0'].c_str()); } printf("\n"); return 0;}

总结:

还是to_ string的用法和c_.str()多多留意吧。

转载地址:https://bridge-killer.blog.csdn.net/article/details/115421743 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:PAT (Advanced Level) 1006 Sign In and Sign Out (25 分)
下一篇:有将-输顿计算实例

发表评论

最新留言

不错!
[***.144.177.141]2024年04月15日 21时51分28秒