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

  1. PAT (Advanced Level) 1005. Spell It Right (20)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

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

  3. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  4. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

  5. PTA(Advanced Level)1025.PAT Ranking

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

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

  7. PTA (Advanced Level) 1008 Elevator

    Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...

  8. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

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

随机推荐

  1. QT中的线程与事件循环理解(1)

    1.需要使用多线程管理的例子 一个进程可以有一个或更多线程同时运行.线程可以看做是“轻量级进程”,进程完全由操作系统管理,线程即可以由操作系统管理,也可以由应用程序管理.Qt 使用QThread 来管 ...

  2. python之随机验证码

    一.内置函数 chr ------把数字转换为字母 ord-------把字母转换为数字 n = chr(65) print (n) m = ord("A") print(m) 二 ...

  3. java 判断手机号码和邮箱的正则表达式

    很多场合会用到判断输入框输入的是否为手机或者邮箱,下面是这个正则表达式: Pattern  patternMailBox  = Pattern .compile( "^([a-zA-Z0-9 ...

  4. Python自动化开发 - Django【进阶篇】

    Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行 ...

  5. Memcached和Memcache安装(64位win2008)

    一.Memcached和Memcache的区别: 网上关于Memcached和Memcache的区别的理解众说纷纭,我个人的理解是: Memcached是一个内存缓存系统,而Memcache是php的 ...

  6. IE兼容性小结(IE7及以上版本)

    缘由 在急速的互联网时代,原本ie系列的低版本浏览器就该淘汰了.可偏偏还是有很多xp系统ie7.8浏览器,这有让网站甚是苦逼.练就了一套新技术,原本以为能大展身手,可却在ie阵容中认怂了,不得不规规矩 ...

  7. CentOS 7配置Let’s Encrypt支持免费泛域名证书

    Let’s Encrypt从2018年开始支持泛域名证书,有效期3个月,目前仅支持acme方式申请,暂不支持certbot. 1.安装acme.sh curl https://get.acme.sh ...

  8. Centos7 下一键安装JDK和Maven

    JDK 1. 使用yum search java|grep jdk 查看jdk版本 2. 选择版本安装 yum install java-1.7.0-openjdk,如果用1.8的,只用改版本号即可. ...

  9. .NET MVC 学习笔记(二)— Bootstrap框架

    二..NET MVC 学习笔记(一)—— Bootstrap框架 在实际开发过程中,页面的样式问题是让人很头疼的问题,良好的用户界面可以大大提高用户体检,而在你没有前端开发人员的火力支援情况下,并不是 ...

  10. openvSwitch 基本命令

    建立ovs接口连接两个namespace组成二层网络 环境搭建拓扑 br0 +--------------------------------------+ +--+ +--+ +---+ | tap ...