PAT (Advanced Level) Practise 1005. Spell It Right (20)

来源:互联网 发布:js e.currenttarget 编辑:程序博客网 时间:2024/06/06 01:55

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
题解:
因为对大整数模板比较熟悉,很自然就想到利用大整数模板,然后再数位求和,最终挺快就AC了。所以,有属于自己的算法模板还是比较重要的,写博客的过程其实也是积累的过程。
代码:
#include <cstdio>#include <cstring>char mp[15][7]={"zero","one","two","three","four","five","six","seven","eight","nine"};struct bign{    int d[105];    int len;    bign(){        memset(d,0,sizeof(d));        len=0;    }};bign change(char s[]){    bign a;    a.len=strlen(s);    for(int i=0;i<a.len;i++) a.d[i]=s[a.len-1-i]-'0';    return a;}int main(){    char s[105];    gets(s);    bign a=change(s);    int sum=0;    for(int i=0;i<a.len;i++) sum+=a.d[i];    int buf[105],num=0;    do{        buf[num++]=sum%10;        sum/=10;    }while(sum);    for(int i=num-1;i>=0;i--){        if(i==num-1) printf("%s",mp[buf[num-1]]);        else printf(" %s",mp[buf[i]]);    }    return 0;}

 
阅读全文
0 0
原创粉丝点击