PAT Basic 1002

1002 写出这个数 (20)(20 分)

读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10^100^。

输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。

输入样例:

1234567890987654321123456789

输出样例:

yi san wu

题解:使用string储存n,遍历一遍该字符串,计算出n的和,使用枚举法输出。
 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<algorithm>
 5 #include<stack>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     string a;
12     stack<int> ac;
13     cin>>a;
14     long int n;
15     for( int i = 0; i < a.length(); i++){
16         n = n + (a[i] - '0');
17     }
18     while(n){
19         int m = n%10;
20         n /= 10;
21         ac.push(m);
22     }
23     while(!ac.empty()){
24         int m;
25         m = ac.top();
26         if( m == 0 )
27         cout<<"ling";
28         if( m == 1 )
29         cout<<"yi";
30         if( m == 2 )
31         cout<<"er";
32         if( m == 3 )
33         cout<<"san";
34         if( m == 4 )
35         cout<<"si";
36         if( m == 5 )
37         cout<<"wu";
38         if( m == 6 )
39         cout<<"liu";
40         if( m == 7 )
41         cout<<"qi";
42         if( m == 8 )
43         cout<<"ba";
44         if( m == 9 )
45         cout<<"jiu";
46         if(ac.size() != 1)
47         cout<<" ";
48         ac.pop();
49     }
50     return 0;
51 }
 

猜你喜欢

转载自www.cnblogs.com/yxp400/p/9445128.html