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 (≤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
题目概述:
给出一个长数字,把每一位的数加起来,然后按英文的0-9输出结果分析:
1.用string存储,否则溢出 2.建立dict对应0-9的英文#includeusing 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年04月15日 21时51分28秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
DM365 应用层gpio控制
2019-04-29
linux i2c子系统abc
2019-04-29
CSS3 帧动画(Sprite,直译叫雪碧图)
2019-04-29
Java 父线程与子线程相互通信的方法
2019-04-29
Redis 六种淘汰策略和三种删除策略
2019-04-29
Java LinkedHashMap
2019-04-29
JPA 多线程同时对一条数据进行Update的问题
2019-04-29
JPA 多线程对数据进行更新,Update和Insert同时存在的问题
2019-04-29
Java 高性能队列Disruptor
2019-04-29
SpringBoot 使用https
2019-04-29
Java 读写锁
2019-04-29
JVM Minor GC、Full GC和Major GC
2019-04-29
SpringBoot @Scheduled 执行两次的问题
2019-04-29
tomcat配置JVM
2019-04-29
Ubuntu软件安装&卸载
2019-04-29
面试笔试易错知识点Java篇八
2019-04-29
弹性事务框架ETF4J——面向Java微服务的交易最终一致性解决方案
2019-04-29
【Scala 教程】Scala 条件与循环语句
2019-04-29
【Scala 教程】Scala 集合类型
2019-04-29