PAT甲题题解-1005. Spell It Right (20)-数位求和,水

把每个位上的数字求和sum,然后以英文单词的形式输出sum的每个位

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
/*
把每个位上的数字求和sum,然后以英文单词的形式输出sum的每个位
水
*/
char word[10][20]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int main()
{
    char str[105];
    scanf("%s",str);
    int len=strlen(str);
    int sum=0;
    for(int i=0;i<len;i++){
        sum+=str[i]-'0';
    }
//printf("sum:%d\n",sum);
    sprintf(str,"%d",sum);
    len=strlen(str);
    int idx=str[0]-'0';
    printf("%s",word[idx]);
    for(int i=1;i<len;i++){
        idx=str[i]-'0';
        printf(" %s",word[idx]);
    }
    return 0;
}
View Code

 

posted @ 2017-04-18 14:57  辰曦~文若  阅读(266)  评论(0编辑  收藏  举报