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

个人最初的题解思路是设定字符串常量,求和得出数值后使用if else计算出各位的数字对应输出:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int sum = 0;
    char str[101];
     const char* a[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
     cin>>str;
     int len=strlen(str);
     for(int i=0;i<len;++i)
     {
        sum = sum+(str[i]-'0');
     }
     //cout<<sum<<endl;
     if(sum>=0&&sum<10)
     {
        printf("%s",a[sum%10]);
     }
     else if(sum>10&&sum<100)
     {
        printf("%s %s",a[sum/10],a[sum%10]);
     }
     else if(sum>100)
     {
        printf("%s %s %s",a[sum/100],a[(sum/10)%10],a[sum%10]);
     }
     return 0;
}

更优的方法是:

#include <iostream>
using namespace std;
int main() {
        string a;
        cin >> a;
        int sum = 0;
        for (int i = 0; i < a.length(); i++)
            sum += (a[i] - '0');
        string s = to_string(sum);
        string arr[10] = {"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"};
        cout << arr[s[0] - '0'];
        for (int i = 1; i < s.length(); i++)
                cout << " " << arr[s[i] - '0'];
        return 0;
}

to_string函数将数字转为字符串完美解决了各个位置上的数值输出问题

PAT甲级——1005.SpellItRight(20分)的更多相关文章

  1. PAT 甲级 1035 Password (20 分)(简单题)

    1035 Password (20 分)   To prepare for PAT, the judge sometimes has to generate random passwords for ...

  2. PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)

    1077 Kuchiguse (20 分)   The Japanese language is notorious for its sentence ending particles. Person ...

  3. PAT 甲级 1061 Dating (20 分)(位置也要相同,题目看不懂)

    1061 Dating (20 分)   Sherlock Holmes received a note with some strange strings: Let's date! 3485djDk ...

  4. PAT甲级——1035 Password (20分)

    To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...

  5. PAT甲级——1061 Dating (20分)

    Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkg ...

  6. PAT甲级——1077.Kuchiguse(20分)

    The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...

  7. 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 ...

  8. PAT 甲级 1005 Spell It Right (20)(代码)

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

  9. pat 1035 Password(20 分)

    1035 Password(20 分) To prepare for PAT, the judge sometimes has to generate random passwords for the ...

随机推荐

  1. jar包-循环遍历-开机启动服务-微服务-多项目拷贝-pid杀死进程-mysql备份脚本-防火墙检测脚本

    vi /root/serverkaiji.sh #!/bin/bash ls /tlvnksc/ | egrep -v "^c|^f" > /root/service.lis ...

  2. SQL约束攻击

    本文转载自https://blog.csdn.net/kkr3584/article/details/69223010 目前值得高兴的是,开发者在建立网站时,已经开始关注安全问题了--几乎每个开发者都 ...

  3. mysql 时区问题导致的时间相差14小时

    1.mysql 字段名称 类型 begin_time TIME begin_time=08:18:39 2.java数据库连接串 jdbc:mysql://x.x.x.x:3306/y?useUnic ...

  4. VScode 修改中文字体

    打开vscode ctrl+,打开设置 找到font,第一个是首选的英文字体,第二个是中文字体.

  5. POJ 3087:Shuffle'm Up

    Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7364   Accepted: 3408 Desc ...

  6. 尝试解决 : Microsoft Visual C++ 14.0 is required 的问题

    当在pycharm  中安装  gevent 的时候 发生了  错误   晚上搜索的时候发现  解决问题有两种 方法 1  是   下载  whl  文件  通过二进制的方式 导入模块的包   想了想 ...

  7. Window NodeJs安装

    1.下载NodeJs 官网下载地址:http://nodejs.cn/download/ ​ 2.安装 双击,全程next安装. 安装完成,在cmd下面执行查看版本命令,命令如下 C:\Users\A ...

  8. Java 使用控制台操作实现数据库的增删改查

    使用控制台进行数据库增删改查操作,首先创建一个Java Bean类,实现基础数据的构造,Get,Set方法的实现,减少代码重复性. 基本属性为 学生学号 Id, 学生姓名 Name,学生性别 Sex, ...

  9. php对象:__autoload()函数及单入口文件,__set(), __get(), get_class_methods(),get_class_vars()

    __autoload():当类中找不到相关类的时候,会自动执行__autoload()函数,可以自动加载相关文件 __set() : 当对类的私有变量进行调用赋值时,自动调用该方法.  __get() ...

  10. count(1),count(*)和count(列)的比较

    转自:https://www.cnblogs.com/Caucasian/p/7041061.html 1.关于count(1),count(*),和count(列名)的区别 相信大家总是在工作中,或 ...