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. Vulkan 开发学习资料汇总

    开发资料汇总 1.API Reference 2.Vulkan Spec 有详细说明的pdf 文章 1.知乎Vulkan-高性能渲染 2.Life of a triangle - NVIDIA's l ...

  2. 005、mysql查询表的结构

    EXPLAIN hcc_ip 如果有一个表,表明为hcc_ip,使用以上语句可以得到下图 不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信二维码打赏任意金额给作者(微信号:38247724 ...

  3. C/C++学习笔记_gdb调试

    1.前提条件:可执行文件包含调试信息 gcc -g 2.gdb 文件名 ---启动gdb调试 3.查看代码的命令 当前文件: list 行号(函数名) 指定文件: list 文件名:行号(函数名)4. ...

  4. 常用sql语句(mysql测试)

    DB数据库,DatabaseDBMS数据库管理系统,DatabaMemanagmentSystemSQL结构化查询语言,structure Query Language 开启服务net start m ...

  5. cmake 简易入门

    目录结构 root -| |--**.cpp |--CmakeList.txt |--current path |--(执行cmake ../) |-- (执行make的目录) 步骤: 1 编写 Cm ...

  6. JSON整理

    1.什么是JSON JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. 2.JSON基于两种结构: (1 )“名称/值“对的集合(A co ...

  7. Codeforces 1290B/1291D - Irreducible Anagrams

    题目大意: 两串字符串 s 和 t 是否 anagrams(下文简称ANA) 的定义是: 是否能将 s 内的字母打乱顺序后再拼接得到 t 我们考虑互相ANA的两串字符串 s 和 t 我们称 t 是 s ...

  8. VUE- 解决插件报错问题

    VUE- 解决插件报错问题 解决办法:先删除node_modules整个文件夹,然后cnpm cache clean,然后cnpm install. cnpm  cache clean时可能会提示 这 ...

  9. 操作实践:maven工程查找工程中多余的jar包

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 版本迭代过程中对jar的依赖可能会产生变化,一些本不必再依赖的jar包可以因为没有清除而依然留在版本的发布 ...

  10. vue的选项卡功能

    选项卡:点击不同的按钮会显示不同的内容 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...