PAT甲1005字符串

PAT甲1005字符串

题目链接
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 (≤10
​100
​​ ).

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

题意:
输入一个字符串,输出所有数字的和
题解:
模拟即可
代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
using namespace std;
string s[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int a[101];
int main()
{
string str;
cin>>str;
int n=0;
for(int i=0;i<str.size();i++)
{
n+=str[i]-'0';
}
int t=0,k=0;
while(n>0)
{
t=n%10;
a[k++]=t;
n/=10;
}
for(int i=k-1;i>0;i--)
cout<<s[a[i]]<<" ";
cout<<s[a[0]];
return 0;
}
-------------本文结束感谢您这么好看还看我的文章-------------