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 ...
随机推荐
- java锁类型
转载链接在每个锁类型后边 线程锁类型 1.自旋锁 ,自旋,jvm默认是10次吧,有jvm自己控制.for去争取锁 锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchroni ...
- jvm虚拟机---执行引擎子系统
Java虚拟机只与Class文件相关联,它规定了Class文件应该具有的格式,而不论该文件是由什么语言编写并编译而来.所以,任何语言只要能够最终编译成符合Java虚拟机要求的Class文件,就可以运行 ...
- Vue的配置
一.build:打包的配置文件的文件夹 1.build.js 生产版本的配置文件,一般这个文件我们是不改的 'use strict' //调用检查版本的文件,check-versions的导出直接是 ...
- [device-orientation] 使用手机设备的方向感应实现图片选择
<div class="main"> <h2>Device Orientation</h2> <table> <tbody&g ...
- Windows核心编程:第8章 用户模式下的线程同步
Github https://github.com/gongluck/Windows-Core-Program.git //第8章 用户模式下的线程同步.cpp: 定义应用程序的入口点. // #in ...
- ajax调用WebMethed返回处理请求时出错
ajax post调用WebMethed报错,返回的信息如下: {“Message”:“处理请求时出错”,“StackTrace”:“”,“ExceptionType”:“”} 查了一下WebMeth ...
- 初识Identity并添加身份验证管理页面
目录 初识Identity并添加身份验证管理页面 前言 什么是ASP.NET Core Identity 创建带有身份验证的WebApp 尝试运行 检查解决方案中的项目文件 发现问题 原因 解决问题 ...
- ubuntu 环境 openstack 源码包制成 deb 包
安装软件: sudo apt-get install dh-make checkinstall cd neutron sudo checkinstall -D -y -install=no -pkgv ...
- html-文件上传设置accept类型延时问题
今天在做文件上传时,采用了jQuery的upload插件,使用过程中发现了一个很有意思也很头疼的问题. 上传按钮,第一次点击时瞬间就可以打开文件选择框,之后再点击则需要等待恐怖的8s以上. 百度 ...
- Java零基础教程(二)基础语法
Java 基础语法 一个 Java 程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作.下面简要介绍下类.对象.方法和实例变量的概念. 对象:对象是类的一个实例,有状态和行为.例如 ...