PAT甲级 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


因为输出顺序的关系,采用递归。递归的时候判断2个条件:1.个位的输出,保证输出格式的正确。2.当n整个等于0时的输出zero。

#include
#include
using namespace std;
const int maxn=100+1;
char strInput[maxn];
char english[10][10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
void outputEnglish(int n,int isFirst){
    if(isFirst) printf("%s",english[n]);
    else printf("%s ",english[n]);
}

void output(int n,int isFirst){
    if(n!=0){
        output(n/10,0);
        outputEnglish(n%10,isFirst);
    }else if(isFirst){
        outputEnglish(n,isFirst);//n=0 print zero
    }
}

int main(){
    //freopen("./in","r",stdin);
    int sum=0;
    scanf("%s",strInput);
    int i;
    for(i=0;strInput[i]!='\0';i++){
        sum+=strInput[i]-'0';
    }
    output(sum,1);
}

你可能感兴趣的:(字符串格式化输出,PAT,PAT甲级)