PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642

题目描述:

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.

译:给出一个非负整数 N , 你的任务是计算出 N 的每位数的和,并且用英语输出和的每一位数。


Input Specification (输入说明):

Each input file contains one test case. Each case occupies one line which contains an N (≤ 10 100 ).

译:每个输入文件包含一个测试用例,每个测试用例包含一个整数 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

The Idea:

题目中说明 N 的范围是 不超过 10 100 的, N 的存储我们采用 string 类型,且最大数位和也只是 999 , 所以用 int 存储完全足够。

存好数位和之后,将数位和转为一个 string 字符串,然后遍历整个字符串,找到对应数字的英文单词即可。

最后亲身教训: 不要把 0 - 9 对应的英文单词写错!!!


The Codes:

#include<bits/stdc++.h>
using namespace std;
string numbers[10] = {"zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" } ;
string s , res ;
int main(){
int ans = 0 ;
getline(cin , s);
for(int i = 0 ; i < s.size() ; i ++) ans += s[i] - '0' ;
res = to_string(ans) ; // 将结果 ans 转为字符串
for(int i = 0 ; i < res.size() ; i++)
cout << numbers[res[i] - '0'] << ((i == res.size() - 1)?"\n":" ") ;
return 0 ;
}

最新文章

  1. TF-IDF算法学习报告
  2. 精心挑选10款优秀的 jQuery 图片左右滚动插件
  3. C语言实现penna模型
  4. JS如何判断包括IE11在内的IE浏览器 &lt;转载&gt;
  5. Linux下删除空文件,删除指定大小的文件
  6. C#类的一些概念
  7. poj1142.Smith Number(数学推导)
  8. POJ 1149 PIGS ★(经典网络流构图)
  9. 【转】iOS 宏(define)与常量(const)的正确使用-- 不错
  10. coco2d-x中的坐标系问题
  11. Problem H: 小火山的围棋梦想 多校训练2(小火山专场)
  12. JavaWeb框架SSH_Struts2_(二)
  13. Python元编程
  14. 用感知机(Perceptron)实现逻辑AND功能的Python3代码
  15. return_fun.go 源码阅读
  16. webpack4的总结
  17. python,接口自动化有几大类
  18. docker常用命令汇总
  19. 基于jQuery虾米音乐播放器样式代码
  20. C#.NET常见问题(FAQ)-delegate委托链如何使用

热门文章

  1. nodejs 调用win32 api
  2. BGV崛起带动DeFi重回大众视野
  3. React Portal - 弹出层的优秀解决方案
  4. C++单链表反转、两有序链表合并仍有序
  5. 用铁电存储器FRAM让穿戴式设备更省电
  6. MySQL5.7.29 和 Navicat ===&gt; windows窗口式按装和使用
  7. Spring注解@PropertySource加载配置文件和SpringBoot注解@Value、@ConfigurationProperties进行属性映射
  8. docker仓库之分布式harbor (一)
  9. python类的内部方法
  10. wxWidgets源码分析(6) - 窗口关闭过程