pat甲级
思路:依次读入每位的和,然后把和按照每位转换为英文单词输出。注意:大数可以当作字符串读入。
训练:字符串操作
题目描述:
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:12345Sample Output:
one five
参考代码:
#include <iostream>
#include <string>
using namespace std;
string eng[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int main()
{
char ch;
int sum=0;
string line="\n";
while(cin>>ch)
sum+=ch-'0';
line=eng[sum%10];
sum/=10;
while(sum)
{
line=eng[sum%10]+" "+line;
sum/=10;
}
cout<<line;
return 0;
}