1005. Spell It Right(20)—PAT 甲级
程序员文章站
2022-03-03 22:16:37
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 Specificat ......
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
题目大意:求一个整数所有位上的数字之和,并用英文对应数字的每一位表示出来。
分析:因为N的位数高达100位,所以用字符串输入并遍历字符串得到所有数字的和,利用c++自带的to_string函数将和转化为整数类型,最后利用常量数组进行输出转换。
#include <iostream> #include <string> using namespace std; const string num[10]={"zero","one","two","three","four","five","six","seven","eight","nine"}; int main() { string str; int sum=0; cin>>str; for(auto c:str) { sum+=c-'0'; } string s=to_string(sum); for(auto c:s) { if(c!=s.front()) cout<<" "; cout<<num[c-'0']; } cout<<endl; return 0; }
推荐阅读
-
1005 Spell It Right (20 分)——13行代码Ac
-
*PAT_甲级_1073 Scientific Notation (20point(s)) (C++)【字符串处理/科学计数法】
-
PAT_甲级_1050 String Subtraction (20分) (C++)【签到题/二分查找/字符串处理】
-
PAT_甲级_1065 A+B and C (64bit) (20分) (C++)【大数相加】
-
PAT 1005 Spell It Right (20分) python实现
-
1005. Spell It Right(20)—PAT 甲级
-
PAT甲级1031 Hello World for U (20分)|C++实现
-
PAT甲级1019 General Palindromic Number (20分)|C++实现
-
PTA甲级考试真题练习5——1005 Spell It Right
-
PAT_甲级_1096 Consecutive Factors (20point(s)) (C++)【分解因子/优美暴力】