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;
}
全部评论

相关推荐

在评审的大师兄很完美:像这种一般就是部门不匹配 转移至其他部门然后挂掉 我就是这样被挂了
点赞 评论 收藏
分享
孤寡孤寡的牛牛很热情:为什么我2本9硕投了很多,都是简历或者挂,难道那个恶心人的测评真的得认真做吗
点赞 评论 收藏
分享
联通 技术人员 总包不低于12
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务