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 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
题目大意

给一个非负数,我们需要得到这个数每一位相加的和,输出结果的每位数用英文表示,每个数字用空格隔开。

题目分析

考虑到输入数字的大小限制为10100,所以考虑使用字符数组存储输入数据,将数组的每一位转换为数字进行累加,最后将输出结果以英文的形式输出。

代码
#include "stdio.h"
#include "stdlib.h"
int main(){
    char N[105];//存储输入数据
    int a[5];
    int num;
    int sum = 0,total;
    int i;
    scanf("%s",N);
    for (i = 0;N[i] != '\0';i++){
        num = N[i]-'0';
        sum += num;
    }//将输入数字的每一位数字进行累加。
    total = sum;
    for (i = 0;total != 0;i++){
        a[i] = total%10;
        total = total/10;
    }//将输出结果按数位展开。
    if (sum == 0){
        i = 1;
        a[0] = 0;
    }
    for (i = i-1;i >=0;i--){
        if (a[i] == 0) printf("zero");
        if (a[i] == 1) printf("one");
        if (a[i] == 2) printf("two");
        if (a[i] == 3) printf("three");
        if (a[i] == 4) printf("four");
        if (a[i] == 5) printf("five");
        if (a[i] == 6) printf("six");
        if (a[i] == 7) printf("seven");
        if (a[i] == 8) printf("eight");
        if (a[i] == 9) printf("nine");
        if (i != 0) printf(" ");//按照输出要求格式输出。
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值