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

相关推荐

10-09 09:39
门头沟学院 C++
HHHHaos:这也太虚了,工资就一半是真的
点赞 评论 收藏
分享
09-29 17:44
已编辑
蔚来_测(准入职员工)
//鲨鱼辣椒:见不了了我实习了四个月上周再投筛选了一天就给我挂了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务