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
题目分析
题目表明给出一个小于等于10100的非负整数,需要将其各位数字之和进行翻译。
输入格式:
非负整数N
输出格式:
依次输出和的每位数字的英文
解决方法:
#include<iostream>
#include<string.h>
using namespace std;
int main(){
string num;
cin>>num;
int sum=0;
for(int i=0;i<num.length();i++){
sum+=num[i]-'0';
}
string str=to_string(sum);
string english[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
for(int i=0;i<str.length()-1;i++){
cout<<english[str[i]-'0']<<" ";
}
cout<<english[str[str.length()-1]-'0']<<endl;
return 0;
}
处理结果: