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 ...
随机推荐
- Linux 下socket通信终极指南(附TCP、UDP完整代码)
linux下用socket通信,有TCP.UDP两种协议,网上的很多教程把两个混在了一起,或者只讲其中一种.现在我把自己这两天研究的成果汇总下来,写了一个完整的,适合初学者参考,也方便自己以后查阅. ...
- POJ2431--Expedition(优先队列)
Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Bein ...
- js-实现双色球功能
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 冲刺博客NO.4
今天开站立会议时,有一点分歧,原本我认为的隐私保护和其他人认为的不一样,在沟通后这部分功能达成共识. 今天做了什么:组员完成了用户输入部分,信息输入.添加了一些组件和活动完善界面. 遇到的苦难,界面 ...
- SRM387 div1
250pt: 题目:有一些盒子(不大于50个),每个盒子里有一些大理石(最多50种颜色),然后给定每个盒子里每种颜色大理石的个数(没有为0),求最少操作几步满足: 1:最多只能一个盒子里有多种颜色,叫 ...
- Django:常见的orm操作
ArticlePost模型对应的表如下: 1.查询两个日期之间2019.04.20到2019.04.25之间的文章 import datetime from.models import Article ...
- 使用 DotNetty 实现 Redis 的一个控制台应用程序
零:Demo 跑出来的结果如图 上图说明 图中左边蓝色的命令行界面,是用windows powershell 命令行链接的. 1.打开powershell命令行界面,输入命令[telnet 127 ...
- Speech Synthesis
<Window x:Class="Synthesizer.MainWindow" xmlns="http://schemas.microsoft.com/winfx ...
- 06_python_小数据池/ is == /编码
一.小数据池 1.代码块 python程序是由代码块构成的.一个代码块的文本作为python程序执行的单元.代码块: 一个模块, 一个函数, 一个类, 甚至每一个command命令都是一个代码块. 一 ...
- re模块 模块
import re findall() 烦的奥 import re # 1. findall 查找所有结果,数据不是特别庞大 lst = re.findall('a','abcsdfasdfa') ...