1005. Spell It Right (20)

原创 2016年08月30日 15:29:05

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 (<= 10100).

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

题目是要求输出一段数字,将各个数字相加得出的和,按位对应输出zero~nine

思路:由于输入的数可能大于长整型,因此选择用一个字符数组来存储,然后循环相加,最后对和循环按10取模,替换成相应的英文,需要注意的是定义的字符数组大小不能太小,输入为0的时候也要能输出zero。

AC代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char number[10][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
char result[10][10] = {0};
int main(int argc, char* argv[])
{
    int sum = 0, i;
    char num[1000] = {0};
    scanf("%s", num);
    for(i = 0; i < strlen(num); i++)
    {
        sum += num[i] - '0';
    }
    if(sum == 0)
    {
        printf("zero\n");
    }
    else{
        i = 0;
        while(sum){
            strcpy(result[i++], number[sum%10]);
            sum = sum / 10;
        }
        for(i = i - 1;i >= 0 ; i--)
        {
            if(0 == i)
                printf("%s\n", result[0]);
            else
                printf("%s ", result[i]);
        }
    }
    system("pause");
    return 0;
}

相关文章推荐

1005. Spell It Right (20)

http://www.patest.cn/contests/pat-a-practise/1005 Given a non-negative integer N, your task...

[PAT]1005. Spell It Right (20)

/************************************************************** 1005. Spell It Right (20) 时间限制 400 ...
  • fnzsjt
  • fnzsjt
  • 2014年11月10日 13:35
  • 760

PAT1005. Spell It Right (20)

1005. Spell It Right (20)时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 判题程序Standard作者CHEN, Yue Given a non-n...

【PAT (Advanced Level)】1005. Spell It Right (20)

1005. Spell It Right (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B...

浙大 PAT Advanced level 1005. Spell It Right

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e...

PAT A1005 Spell It Right

//解答和我想的是一样的,要注意是二维数组的map即可 //输出change的那里一开始写成了%c,警示! //第一次提交,有一个3分的点错误 //第二次提交,除了0那个测试点其他全部错误,发现把...

PAT_1005: Spell It Right

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e...

【PAT】1005 Spell It Right

题目:http://pat.zju.edu.cn/contests/pat-a-practise/1005 分析:简单题。将输入的字符串一个个的转换成数字再相加,然后将相加的结果用英文打印出来就可以。...

1005. Spell It Right 解析

感觉没什么好说的。我是偷懒用String读入,然后转换计算。代码看上去比较麻烦。 附上AC代码: #include #include #include using namespace st...

浙江大学PAT_甲级_1005.1005. Spell It Right (20)

c++实现
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:1005. Spell It Right (20)
举报原因:
原因补充:

(最多只允许输入30个字)