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. GitHub 网站上不去/加载慢/加载不全 解决办法

    1. 当你打开你的 GitHub 2. F12 进入检查页面,点击 network 3. 找到变红的字段右键复制连接 4. 打开 DNS 查询网站,输入你复制的网址,点击查询 5. 选择国内的 ip ...

  2. 吴裕雄--天生自然Django框架开发笔记:Django Admin 管理工具

    Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.可以在项目的 settings.py 中的 INSTALLED_APPS 看到它: ...

  3. Android自定义View——QQ音乐中圆形旋转碟子

    1.在onMeasure中测量整个View的宽和高后,设置宽高 2.获取我们res的图片资源后,在ondraw方法中进行绘制圆形图片 3.通过Handler发送Runnable在主线程中更新UI,达到 ...

  4. 使用文件流创建File文件和目录以及其他的一些操作

    我们创建文件时可以直接通过File f=new File(path)来创建一个文件对象,然后再通过 f.createNewFile() 就创建出来了一个文件.比如设置 path 为 C:\Users\ ...

  5. MySQL中间件介绍

    360 Atlas Atlas是由 Qihoo 360, Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它是在mysql-proxy 0.8.2版本的基础上,对其进行了优化 ...

  6. 直击JDD | 京东开启技术服务元年:携手合作伙伴,共创产业未来

    11月19日,主题为"突破与裂变"的2019京东全球科技探索者大会(JDDiscovery)在京盛大开幕.京东集团副总裁黎科峰在JDD主论坛做了题为"技术驱动.开放赋能& ...

  7. css实现高度自适应

    要求.上部固定高度50px,下部分自适应剩下整个屏幕 html, body { height: 100%; margin: 0px; padding: 0px; } #main { backgroun ...

  8. nginx常用编译参数

    ./configurate --prefix=/app/tengine --user=www --group=www --with-http_v2_module --with-http_ssl_mod ...

  9. OpenMP笔记(六)

    OpenMP有三种常见的加锁操作: critical是OpenMP的指令,它规定其后的代码为临界块,任何时候只允许一个线程访问: omp_set_lock是OpenMP的库函数,要跟omp_unset ...

  10. Python说文解字_父类的继承

    1. 第一个问题: 我们知道类是可以继承其他类的,在继承的过程中我们不光可以继承父类的方法,还可继承父类的属性,另外还可以在父类的基础上添加自己的东西. 2. 第二个问题: 我们继承父类属性和方法的时 ...