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 ...
随机推荐
- WebAssembly All In One
WebAssembly All In One wasm https://webassembly.org/ https://developer.mozilla.org/en-US/docs/WebAss ...
- Linux & terminal color & command line color
Linux & terminal color & command line color how to change Linux terminal color https://askub ...
- Linux & SIGUSER1
Linux & SIGUSER1 https://stackoverflow.com/questions/10824886/how-to-signal-an-application-witho ...
- PBN旁切转弯的精确化计算
PBN转弯保护区中使用频率最高的当属旁切转弯,风螺旋的精确算法会对旁切转弯的绘制带来哪些变化,通过今天这个例子我们来了解一下. 图III-3-2-3 旁切转弯保护区 一.基础参数: ICAO816 ...
- Azure Functions(二)集成 Azure Blob Storage 存储文件
一,引言 上一篇文章有介绍到什么是 SeverLess ,ServerLess 都有哪些特点,以及多云环境下 ServerLess 都有哪些解决方案.在这众多解决方案中就包括 Function App ...
- 26_ mysql数据操作语言:DELETE语句
-- DELETE语句 -- 删除10部门中,工龄超过20年的员工记录 DELETE FROM t_emp WHERE deptno=10 AND DATEDIFF(NOW(),hiredate)/3 ...
- 处理XML数据应用实践
摘要:GaussDB(DWS)支持XML数据类型及丰富的XML解析函数,可实现关系数据和XML数据的映射管理功能. XML概述 XML是可扩展的标识语言(eXtensible Markup Langu ...
- CentOS7安装Kibana7.9.2
1:下载 wget https://artifacts.elastic.co/downloads/kibana/kibana-7.9.2-linux-x86_64.tar.gz 点击进入官网 安装手册 ...
- redis数据结构和对象一
1. SDS:简单动态字符串(simple dynamic string) Redis没有直接使用C语言的字符串,而是自己构建了一种名为简单动态字符串类型,并将SDS用作Redis的默认字符串. SD ...
- 翻译:《实用的Python编程》03_01_Script
目录 | 上一节 (2.7 对象模型) | 下一节 (3.2 深入函数) 3.1 脚本 在该部分,我们将深入研究编写 Python 脚本的惯例. 什么是脚本? 脚本就是运行和终止一系列语句的程序. # ...