1005 Spell It Right (20)(20 分)

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 (<= 10^100^).

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

思路:

每读入一个字符,就拿该字符与‘0’相减,然后与sum加和。读取完毕之后,如果sum等于0,则直接将sum压入栈;如果sum大于0,则对sum进行循环除十取模,将模存储于堆栈中。然后把0-9十个英文单词存储于一个数组之中,最后每从堆栈中弹出一个模,就输出一个对应的英文单词。

复杂度:

时间复杂度为O(n)。空间复杂度为O(1)。
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
char a[];
while(cin>>a)
{
int s=;
int l=strlen(a);
for(int i=;i<l;i++)
{
s+=(int)(a[i]-'');
}
stack<int>q;
while(s>=)
{
int a=s%;
q.push(a);
s/=;
}
q.push(s);
int k=;
while(!q.empty())
{
if(k!=)
cout<<" ";
k++;
int i=q.top();
q.pop();
if(i== )
cout<<"zero";
if(i==)
cout<<"one";
if (i==)
cout<<"two";
if (i==)
cout<<"three";
if (i==)
cout<<"four";
if (i==)
cout<<"five";
if (i==)
cout<<"six";
if (i==)
cout<<"seven";
if (i==)
cout<<"eight";
if (i==)
cout<<"nine";
}
cout<<endl;
}
return ;
}
 

PTA 1005 Spell It Right (20)(20 分)水题的更多相关文章

  1. PAT 甲级 1005 Spell It Right (20 分)

    1005 Spell It Right (20 分) Given a non-negative integer N, your task is to compute the sum of all th ...

  2. 1005 Spell It Right (20 分)

    1005 Spell It Right (20 分) Given a non-negative integer N, your task is to compute the sum of all th ...

  3. 1005 Spell It Right(20 分)

    1005 Spell It Right(20 分) Given a non-negative integer N, your task is to compute the sum of all the ...

  4. 1005. Spell It Right(20)—PAT 甲级

    Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...

  5. PTA 1005 Spell It Right

    题目描述: Given a non-negative integer N, your task is to compute the sum of all the digits of N, and ou ...

  6. PTA | 1005 继续(3n+1)猜想 (25分)

    卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对 n=3 进行验证的时 ...

  7. PAT 1005 Spell It Right

    1005 Spell It Right (20 分)   Given a non-negative integer N, your task is to compute the sum of all ...

  8. PAT甲1005 Spell it right【字符串】

    1005 Spell It Right (20 分) Given a non-negative integer N, your task is to compute the sum of all th ...

  9. 1005 Spell It Right (20分)

    1005 Spell It Right (20分) 题目: Given a non-negative integer N, your task is to compute the sum of all ...

随机推荐

  1. 那如何判断页面是在移动端还是PC端打开的

    网上有很多方法,写的或难或简单,实际上一行代码就够了. window.location.href = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navi ...

  2. vue的v-model绑定数据问题

    在我的项目中有一个这样的功能模块: 我们修改数据全是在前台的table中修改,然后把这个table的值带到后台.(理想:修改值后,点击确认值改变,点击取消值恢复原来的值)那么问题就来了: a)你修改值 ...

  3. core文件介绍

    原文链接:http://team.eyou.com/?p=27 如有侵犯您的版权,请联系windeal12@qq.com linux下,产生core文件,和不产生core文件的条件: 当我们的程序崩溃 ...

  4. 【PL/SQL编程】循环语句

    1. loop语句 loop plsql_sentence; exit when end_condition_exp; end loop; loop语句会先执行一次循环体,然后再判断“exit whe ...

  5. jsp中把js变量赋给java变量,或者将java变量赋给js变量怎么做?

    在jsp中经常会遇到把js变量赋给java变量,或者将java变量赋给js变量的情况,在此将通用的处理方法小结如下: java变量传给js好办,var a=”<%=javaParam%>“ ...

  6. 使用jQuery操作DOM(1)

    1.常见方法 css(“属性”,”属性值”); //设置单个样式 css({属性1:属性值1,属性2:属性值3...}); //设置多个样式 addClass(“样式名”); //追加单个样式 add ...

  7. C语言学习之指针

    指针这块,看了好久才有点头绪,稍微有点理解了. 一.指针申明以及赋值 int *p; 这样就声明了一个int型的指针p,这个p就是指针变量,可能上述的书写方式会混淆我们队指针的理解(*号) 我们可以这 ...

  8. Vim技能修炼教程(4) - 基本功

    基本功 前面我们学会了插件管理器和如何实现语法高亮,相信大家已经从中体会到了vim插件的强大功能.现在,是时候回来补一补基本功了. Vi有三种主要模式,正常模式,插入模式和可视化模式.正常我们推荐的方 ...

  9. Qt jsoncpp 对象拷贝、删除、函数调用 demo

    /*************************************************************************************************** ...

  10. 实战:向GitHub提交代码时触发Jenkins自动构建

    当我们提交代码到GitHub后,可以在Jenkins上执行构建,但是每次都要动手去执行略显麻烦,今天我们就来实战Jenkins的自动构建功能,每次提交代码到GitHub后,Jenkins会进行自动构建 ...