美文网首页
A1005 Spell It Right (20分).cpp

A1005 Spell It Right (20分).cpp

作者: km15 | 来源:发表于2020-01-17 12:45 被阅读0次

考察:数字和转为单个位的英文单词输出

learn && wrong:
1、数组老是不给名字!以及老是用错数组的名字,这里数组是ans,而不是sum,是ans[]
2、果然出错就优先检查边界值,发现0拼错了

答案技巧
3、N小于等于10的100次方,而最大和每位都是9,所以数位和最大会有100 * 9 = 900,
4、sprintf函数

*/

编程思想:
1、非负整数,但是100次方,太大了,超过long long了!只能数组来做了,所以就是字符数组来做。用string来做,我一直都用string而不是char数组做的
2、第二点就是,英语字符做成数组,来调用,从0到9

#include <iostream>
#include <string>
#include <vector>
using namespace std;

char c[10][10] = { "zero", "one","two","three","four","five","six","seven","eight","nine", };

int main()
{
    string s1;
    cin >> s1;
    int sum = 0;
    
    for (int i = 0;i < s1.size();++i) { //计算和
        sum += s1[i] - '0';
    }

    int ans[200];int num = 0;
    
    //可是得逆序呀!还是得开个数组
    do { //输出英语字母
        ans[num++] = sum % 10;
        sum /= 10;
    } while (sum != 0);

    for (int i = num - 1;i >= 0;--i) {
        printf("%s", c[ans[i]]);
        if (i != 0) printf(" ");
    }
    return 0;

}

相关文章

  • A1005 Spell It Right (20分).cpp

    考察:数字和转为单个位的英文单词输出 learn && wrong:1、数组老是不给名字!以及老是用错数组的名字,...

  • PAT A1005 Spell It Right (20)

    PAT A1005 Spell It RightGiven a non-negative integer N, y...

  • 2019-08-27 A1005 Spell It Right

    也是一道相对简单的题目,这道题容易出错的点是字符串与整型的转换: 不要忘记op[re[i]] - '0'。

  • 1005 Spell It Right

    1005 Spell It Right (20)(20 分) Given a non-negative integ...

  • 1005 Spell It Right

    题目简述 Given a non-negative integer N, your task is to comp...

  • 1005 Spell It Right

    题目 输入一个数字N,N<=10^100。将各位数字相加,最后输出相加和的各位数字的英文,两个连续的单词之间必须有...

  • PAT甲级1005

    1005 Spell It Right (20分)Given a non-negative integer N, ...

  • 【PAT A1005】

    1005 Spell It Right (20)(20 分)Given a non-negative intege...

  • A_1005

    1005 Spell It Right (20)(20 分) Given a non-negative integ...

  • PAT 1005 Spell It Right (20 分)

    PAT 1005 Spell It Right (20 分) Given a non-negative integ...

网友评论

      本文标题:A1005 Spell It Right (20分).cpp

      本文链接:https://www.haomeiwen.com/subject/vduczctx.html