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
题意
对各位数字求和,并按照格式输出
分析
输入到string中,逐位求和
代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
string nums[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
char num[101];
int printNum[101];
int sum;
int main(){
scanf("%s",num);
int len=strlen(num);
sum=0;
for(int i=0;i<len;i++){
sum+=num[i]-'0';
}
int size=0;
if(sum==0){
size=1;
}
while(sum!=0){
printNum[size]=sum%10;
sum/=10;
size++;
}
for(int i=size-1;i>=0;i--){
printf("%s",nums[printNum[i]].c_str());
if(i!=0)
printf(" ");
}
return 0;
}
本文介绍了一道编程题目1005SpellItRight的解题方法,任务是对给定的非负整数N的每一位数字求和,并用英文单词输出每位数字的和。文章详细解析了输入、输出规范,提供了使用C++实现的代码示例,通过字符串逐位求和并转换为英文表示。
3786

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



