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的更多相关文章

  1. 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 ...

  2. PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Android APP 多端适配

    Android APP 多端适配 传统的多终端适配方案,是为大尺寸 Pad开发一个特定的 HD版本. 但是目前支持 Android 系统的设备类型越来越丰富,不同类型的设备尺寸也越来越多样化,特定的H ...

  2. Flutter: IntrinsicWidth类,将孩子的宽度调整为孩子的最大内在宽度

    文档 原文 class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePage ...

  3. Flutter:发布包

    [package] 生成包含模块化Dart代码的可共享Flutter项目 [plugin] 生成一个可共享的Flutter项目, 在Dart代码中包含带有API的API, 针对Android的平台特定 ...

  4. 云原生系列5 容器化日志之EFK

    上图是EFK架构图,k8s环境下常见的日志采集方式. 日志需求 1 集中采集微服务的日志,可以根据请求id追踪到完整的日志: 2 统计请求接口的耗时,超出最长响应时间的,需要做报警,并针对性的进行调优 ...

  5. JAVA 批量下载服务器文件到本地指定文件夹并重命名

    /** * @功能 下载文件到指定文件夹并重命名 * @param url 请求的路径 * @param filePath 文件将要保存的目录 * @param filename 保存到本地的文件名 ...

  6. 通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空

    通过CollectionUtils工具类判断集合是否为空 先引入CollectionUtils工具类: import org.apache.commons.collections4.Collectio ...

  7. oracle 查看 FK constraint referenced_table及columns

    select uc.table_name, uc.r_constraint_name, ucc.table_name, listagg(ucc.column_name, ',') within gro ...

  8. 后端程序员之路 31、Protocol Buffer

    google/protobuf: Protocol Buffers - Google's data interchange formathttps://github.com/google/protob ...

  9. PHP代码审计_用==与===的区别

    目录 背景介绍 如何审计 绕过案例1 绕过案例2 背景介绍 比较==与===的差别 == 是等于符号,=== 是恒等于符号,两个符号的功能都是用来比较两个变量是否相等的,只不过两个符号的比较维度不一样 ...

  10. POJ-1502(基本dijikstra算法)

    MPI Maelstrom POJ-1502 这题是求最短路,但是因为一开始看错题目,导致我去使用prime算法求最小生成树 题意是指一台机器发出信息后,还可以向其他的机器发送信息,所以不能使用pri ...