PAT 1005

题目 : Spell It Right

分值 : 20
难度 : 简单题
思路 : 利用字符串 然后 s[i]-'0' 搞大数
坑点 : 首先 大数,你用int搞不定全部点;其次边界问题:当输入只有0,你输出“zero”吗?
评语 : 先被 大数搞了一波,我用long int; 然后被边界问题:输入只有0搞了一波。很伤心!

具体代码如下

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
using namespace std ;
int flag = 0 ;
void print(int n )
{
if(n!=0)
{
print(n/10) ;
if(flag++)
cout<<" " ;
switch (n%10) {
case 0 : cout <<"zero"; break;
case 1 : cout <<"one" ;break ;
case 2 : cout <<"two"; break;
case 3 : cout <<"three"; break;
case 4 : cout <<"four"; break;
case 5 : cout <<"five"; break;
case 6 : cout <<"six"; break;
case 7 : cout <<"seven"; break;
case 8 : cout <<"eight"; break;
case 9 : cout <<"nine"; break;
}

}
}
int main() {
string n ;
cin >> n ;
//cout << n ;
long int m = 0 ;
for(int i = 0 ; i< n.length() ; i++)
{
m+= (n[i]-'0') ;
}
print(m) ;
if(m==0)
cout<<"zero";
cout<<endl ;
}