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. Visual Studio编译与调用DLL方法

    参考自博客:http://www.cnblogs.com/houkai/archive/2013/06/05/3119513.html 用visual studio 2013新建win32 appli ...

  2. 2017.10.24 A test error about ATE device

    1  A misunderstands  on E-mail Customer: The initial red blink just means theXXX  unit has not yet s ...

  3. 防火墙---iptables

    一.iptables的说明及环境安装 (1)理论基础:当主机收到一个数据包后,数据包先在内核空间中处理,若发现目的地址是自身,则传到用户空间中交给对应的应用程序处理,若发现目的不是自身,则会将包丢弃或 ...

  4. 添加机构organizations模块

    startapp organizations models内容: from django.db import models from datetime import datetime # Create ...

  5. 一张图说明HTTPS 握手过程

  6. python常用模块之shelve模块

    python常用模块之shelve模块 shelve模块是一个简单的k,v将内存中的数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据类型 我们在上面讲json.pickle ...

  7. PHP语法笔记二

    日期函数 date_default_timezone_set(“PRC”):时区设置为中国区 date(“日期格式”[,时间戳]):设置当前或某个时间戳的日期格式. 参数 format 表示时间格式化 ...

  8. js之数码时钟加随机变色

      HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  9. 【机器学习】集成学习之sklearn中的xgboost基本用法

    原创博文,转载请注明出处!本文代码的github地址    博客索引地址 1.数据集 数据集使用sklearn自带的手写数字识别数据集mnist,通过函数datasets导入.mnist共1797个样 ...

  10. jQuery radio|checkbox的取值与赋值

    文章简单即是美[我说的是技术博客] |--radio   |--checkbox   参考: http://blog.csdn.net/gd2008/article/details/6951208 h ...