1002-写出这个数
leenickzh Lv4

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

输入格式

每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于

输出格式

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

输入样例

1
1234567890987654321123456789

输出样例

1
yi san wu

分析

用字符串接受输入,再把字符转为数字累加求和,求完的和再转为字符串,定义一个数组,存放“ling”,”yi”,…,”jiu”.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

int main()
{
string str;
cin>>str;
int sum = 0;
for(int i=0;i<str.size();i++)
{
sum+=str[i]-'0';
}
string s = to_string(sum);
string ex[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
int i;
for(i=0;i<s.size()-1;i++)
{
cout<<ex[s[i]-'0']<<" ";
}
cout<<ex[s[i]-'0'];
return 0;
}
  • Post title:1002-写出这个数
  • Post author:leenickzh
  • Create time:2021-04-13 21:39:54
  • Post link:https://nickk.cn/2021/04/13/1002-写出这个数/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
 Comments