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<iostream>
#include<stdio.h>
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);
}

本文介绍了一个程序设计问题,即给定一个非负整数N,计算其所有数字的和,并将该和的每一位数字用英文单词形式输出。文章提供了一个使用递归方法实现的C++代码示例。
312

被折叠的 条评论
为什么被折叠?



