PAT甲级真题(字符串)——1005 Spell It Right (20 分)
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 (≤1e100).
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<string>
using namespace std;
string arr[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
void print(int num,int flag){
if(num/10)
print(num/10,1);
cout<<arr[num%10];
if(flag)
cout<<" ";
}
int main()
{
string s;
cin>>s;
int num=0;
for(int i=0;i<s.size();i++)
num+=s[i]-'0';
print(num,0);
return 0;
}