PAT-A 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 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

程序代码:

#include<stdio.h>
#include<string.h>
#define MAX 101
char c[MAX];
char ans[MAX]={0};
char set[][10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
void num_to_char(int n);
int main()
{
    scanf("%s",c);
    int len = strlen(c),i,sum = 0;
    for(i=0;i<len;i++)
    {
        sum = sum + c[i]-'0';
    }
    num_to_char(sum);
        len = strlen(ans);
        for(i=len-1;i>=0;i--)
        {
                printf("%s",set[ans[i]-'0']);
                if(i!=0)
                        putchar(' ');
        }   
    return 0;
}
void num_to_char(int n)
{
    if(n==0)
    {
        ans[0] = '0';
        ans[1]= '\0';
        return;
    }
    int i=0;
    while(n!=0)
    {
        ans[i] = n%10 + '0';
        i++;
        n=n/10;
    }
    ans[i] = '\0';
    return;
}
全部评论

相关推荐

点赞 评论 收藏
分享
2025-12-27 16:01
重庆大学 Java
蛊界Go学长林剑行:项目部分,不光要展示技术栈的熟练程度,还要有架构意识+产品意识。知道每个业务逻辑落地的成果和技术选型的思考,不然纯炫技是没意义的,毕竟你用的这些技术大概率跟大厂实际项目不垂直,面试官不一定有兴趣
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务