PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642
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 ;
}
PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642的更多相关文章
- PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642
PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...
- PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642
PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...
- PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642
PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642 题目描述: With the 2010 FIFA World Cu ...
- PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642
PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642 题目描述: Calculate a+b and output the sum i ...
- PAT (Advanced Level) Practice 1005 Spell It Right (20 分) (switch)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...
- PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) (进制转换,回文数)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT (Advanced Level) Practice 1027 Colors in Mars (20 分)
People in Mars represent the colors in their computers in a similar way as the Earth people. That is ...
- PAT (Advanced Level) Practice 1054 The Dominant Color (20 分)
Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits of i ...
- PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) (找最值)
With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...
随机推荐
- js console.log all in one
js console.log all in one this & arguments "use strict"; /** * * @author xgqfrms * @li ...
- computer network layers architecture (TCP/IP)
computer network layers architecture (TCP/IP) 计算机网络分层架构 TCP/IP 协议簇 OSI 模型(7 层) TCP/IP (4 层) Applicat ...
- js 反应&行动
反应 class Reaction { _page = 1; get page() { return this._page; } set page(newValue) { this._page = n ...
- Flutter: Draggable和DragTarget 可拖动小部件
API class _MyHomeState extends State<MyHome> { List<Map<String, String>> _data1 = ...
- 区块链项目NGK未来价值几何?
没有人可以预知NGK未来会涨到多少钱,就像比特币只有10美分时,也无法预测它会涨到现在的价格⼀样.那时候人们把CPU超频挖矿只作为⼀种爱好和娱乐.所以,人们也没有办法预知NGK未来的价格.但可以知道的 ...
- (数据科学学习手札108)Python+Dash快速web应用开发——静态部件篇(上)
本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 这是我的系列教程Python+Dash快速web ...
- 处理ios键盘弹出按钮点击click失效
用ontouchstart绑定事件即可,然后用 document.activeElement.blur();让键盘收起 this.value = value.replace(/\s/g, " ...
- dev 控件获得所有的EFDEVGRID
//获得当前Grid DevExpress.XtraGrid.GridControlNavigator GCN2 = sender as DevExpress.XtraGrid.GridControl ...
- 【转载】Win10彻底格式化磁盘防止数据恢复的技巧
转载地址 注意 要尽量删除数据,请在运行cipher /w时关闭其他所有应用程序. 1.如果你在格式化磁盘后想要防止数据被恢复, Format 命令,而现在只需在其后添加 /P 参数,即可用随机数据覆 ...
- mybites框架遇到的坑之Mapper.xml文件不要随意加注释和ORA-00911
原文链接:https://blog.csdn.net/streetlight8023/article/details/69388495/ 先说解决方法: org.mybatis.spring.MyBa ...