1. 程式人生 > 其它 >1005 Spell It Right (20 分)

1005 Spell It Right (20 分)

Given a non-negative integerN, your task is to compute the sum of all the digits ofN, 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 anN(≤).

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<bits/stdc++.h>
using namespace std;
const int maxn=10010;
int nums[maxn];
void trance(int m){
    if(m==0){
        printf("zero");
    }
    else if(m==1){
        printf("one");
    }
    else if(m==2){
        printf(
"two"); } else if(m==3){ printf("three"); } else if(m==4){ printf("four"); } else if(m==5){ printf("five"); } else if(m==6){ printf("six"); } else if(m==7){ printf("seven"); } else if(m==8){ printf("eight
"); } else if(m==9){ printf("nine"); } } int main(){ string n; cin>>n; if(n=="0"){ printf("zero\n"); return 0; } long long sum=0; for(int i=0;i<n.size();i++){ sum+=n[i]-'0'; } int i=0; while(sum>0){ nums[i]=sum%10; sum/=10; i++; } for(int j=i-1;j>=0;j--){ trance(nums[j]); if(j>0){ printf(" "); } else{ printf("\n"); } } return 0; }