PTA (Advanced Level) 1005 Spell It Right
Spell It Right
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 (≤).
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
题目解析
本题给出一个小于10的100次方的数字,要求计算其每一位的数字的和,并由最大为开始以英文输出和的每一位。
将给出的数字记录在字符串num中计算字符串每一位减去字符’0’后的和即可得到给出数字每一位的和,以字符串数组str[ ]记录其下标数字对应的英文单词。
最后将得到的和每一位对应的单词记录并输出即可。
#include <bits/stdc++.h>
using namespace std;
const string str[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int getSum(string num){ //计算给出数字每一位的和
int ans = ;
for(auto i : num)
ans += i - '';
return ans;
}
int main()
{
string num;
cin >> num; //输入num
int sum = getSum(num); //求每一位的和
vector<string> ans; //记录sum每一位对应的英文单词
do{ //由于可能出现sum为0的情况,所以用do while
ans.push_back(str[sum % ]);
sum /= ;
}while(sum);
reverse(ans.begin(), ans.end()); //由于记录时时由低位到高位记录,所以将其反转ans
for(int i = ; i < ans.size(); i++){ //输出答案
if(i != )
printf(" ");
cout << ans[i];
}
return ;
}
PTA (Advanced Level) 1005 Spell It Right的更多相关文章
- PAT (Advanced Level) 1005. Spell It Right (20)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- PTA(Advanced Level)1036.Boys vs Girls
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
- PTA(Advanced Level)1025.PAT Ranking
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- PTA (Advanced Level) 1009 Product of Polynomials
1009 Product of Polynomials This time, you are supposed to find A×B where A and B are two polynomial ...
- PTA (Advanced Level) 1008 Elevator
Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- PTA (Advanced Level) 1006 Sign In and Sign Out
Sign In and Sign Out At the beginning of every day, the first person who signs in the computer room ...
随机推荐
- android 增量更新原理
原理如下:服务器端设计增量表,记录数据操作顺序id,和增删改查信息.在进行数据库表操作的时候同时进行将信息保存在增量表. android客户端在请求的时候上传最后保存的id.服务端判断最后的id,返回 ...
- Java方法、构造方法的重载;创建对象;调用方法
方法的重载 概念:多个同名但是不同参数的方法称为方法的重载 作用:编译器会根据调用时传递的实际参数自动判断具体调用的是哪个重载方法 特点:方法名相同:同一作用域:参数不同:数量不同 类型不同 顺序不同 ...
- hdu 5086 数列连续和求和
http://acm.hdu.edu.cn/showproblem.php?pid=5086 求一段数列里面所有连续和的和,卡精度 规律很明显,数列里面每个数都被加了i*(n+1-i)次 注意下精度即 ...
- 使用UTL_HTTP时遭遇ORA-29273
http://blog.itpub.net/8520577/viewspace-1295182/ 项目中需要使用utl_http访问webserivce.使用utl_http时报错ORA-29273. ...
- 用 select 语句实现递归的方法
with Test_Recursion(Id,ParentId)AS(select Id,ParentId from [V_KPI_DetailsActivities] where ParentId ...
- [leetcode 12] Inter to Roman
1 题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- SSD 相关基础知识
SDD 基础知识 SSD(Solid State Drives)是固态硬盘,使用闪存颗粒来存储数据,闪存又可分为NAND Flash和NOR Flash,通常所说的SSD硬盘都使用NAND Flash ...
- intellij 快捷键整理
[常规] Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表达式时按 “!”键 Ctrl+E,最近的文件 Ctrl+Shift+E,最近更改的文件 Shift+Click,可以关 ...
- iis支持asp.net4.0的注册命令使用方法
32位的Windows: 1. 运行->cmd 2. cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 3. aspnet_regiis.exe ...
- 【转】selenium webdriver三种等待方法
原文:https://www.cnblogs.com/lgh344902118/p/6015593.html webdriver三种等待方法 1.使用WebDriverWait from seleni ...