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. MySQL Batched Key Access

    Batched Key Access是MySQL 5.6 版本中的新特性,是一种用户提高表join性能的算法.[Batched Key Access]       对于多表join语句,当MySQL使 ...

  2. Linux服务器通过拷贝的方式安装多个tomcat

    Tomcat占用资源少.运行速度快.安装配置简单,在个人开发中拥有广泛的使用者.很多人在使用中存在以下的误区:1.Tomcat必须通过eclipse启动2.Tomcat必须通过安装才能使用运行3.一台 ...

  3. about system (pause) in cpp

    Which is best way to pause the console in C++ programs? using cin.get() or using system("pause& ...

  4. sar工具使用详细介绍

    一:命令介绍:参考资料:http://linux.die.net/man/1/sar sar(System ActivityReporter系统活动情况报告)是目前Linux上最为全面的系统性能分析工 ...

  5. js函数的伪重载

    这也是今天写东西是遇到的一个问题,导致我联想起了函数重载的问题. 在javascript中是没有函数重载机制的,对于用惯了java开发的同学可能就表示吃惊了,我屮艸芔茻,函数 没有重载?那怎么搞?!! ...

  6. gradle Could not create service of type CrossBuildFileHashCache using BuildSessionScopeServices.crea

    gradle Could not create service of type CrossBuildFileHashCache using BuildSessionScopeServices.crea ...

  7. strip()函数和 split()函数

    一:python strip()函数介绍 函数原型:strip可以删除字符串的某些字符 声明:s为字符串,rm为要删除的字符序列 s.strip(rm)        删除s字符串中开头.结尾处,位于 ...

  8. android 几个开源项目

    android的几个开源项目ormlite.volley.jsoup.vitamio ksoap2

  9. js之简易计算器

    <!DOCTYPE html PUBLIC "-//W3C//Dli XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. Linux服务器运行环境搭建(三)——MySQL数据库安装

    官网:http://www.mysql.com/ 官网下载地址:http://dev.mysql.com/downloads/mysql/ 说明:官网下载页面的“Select Platform” 选择 ...