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. POJ1065--Wooden Sticks(动态规划)

    There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The st ...

  2. POJ3176--Cow Bowling(动态规划)

    The cows don't use actual bowling balls when they go bowling. They each take a number (in the range ...

  3. spring-mvc源码阅读笔记

    简要的做一些spring-mvc部分的源码学习笔记 Spring-mvc做的工作主要是俩大方面吧:一个是初始化一个ioc容器,一个是mvc部分的控制和视图模块的实现. 先说下ioc容器的初始化部分:i ...

  4. 使用Xshell在Windows系统和Linux系统之间进行文件传输

    版权声明:本文为转载内容. 原博客内容https://blog.csdn.net/love666666shen/article/details/75742077 Windows系统在安装虚拟机cent ...

  5. Linux分区之parted命令

      之前使用最多的分区命令无疑是fdisk了,大多数情况下fdisk可以满足日常工作上的需求,极个别情况就需要使用parted命令了,至于及个别情况就要从MBR和GPT说起. MBR主引导扇区   主 ...

  6. 实验6 LCD接口

    1.利用单片机控制LCD1602,在LCD1602上显示字符串,并使其整屏左移. #include<reg51.h> #define uchar unsigned char #define ...

  7. B/S FastReprot使用

    FastReport 交流群 群   号:554714044 前言 由于公司开发新产品,前后端分离.netcore +Angular ,之前C/S项目一直使用FastReport ,考虑到员工切换比较 ...

  8. .net core 与ELK(1)安装Elasticsearch

    1.安装java jdk [elsearch@localhost bin]$ java -version openjdk version "1.8.0_181" OpenJDK R ...

  9. 一个简单文本分类任务-EM算法-R语言

    一.问题介绍 概率分布模型中,有时只含有可观测变量,如单硬币投掷模型,对于每个测试样例,硬币最终是正面还是反面是可以观测的.而有时还含有不可观测变量,如三硬币投掷模型.问题这样描述,首先投掷硬币A,如 ...

  10. cad2019卸载/安装失败/如何彻底卸载清除干净cad2019注册表和文件的方法

    cad2019提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装cad2019失败提示cad2019安装未完成,某些产品无法安装,也有时候想重新安装cad2019 ...