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

输入格式:

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

输出格式:

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

输入样例:

1234567890987654321123456789
输出样例:

yi san wu

代码:

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

int main(){
string str[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
string s;
cin>>s;
int sum=0;
for(int i=0;i<s.length();i++)
sum +=(s[i]-'0');//变成int型
string str_sum =to_string(sum);
for(int i=0;i<str_sum.length();i++){
cout<<str[str_sum[i]-'0'];
if(i!=str_sum.length()-1){
cout<<" ";
}
}
return 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
import java.util.*;

public class Main {
public static void main(String[] args) {
String[] str = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int sum = 0;
for(char c : s.toCharArray()){
sum += c-'0';
}
//开始读sum的值,进行输出,注意后面没有空格
Stack<String> stack = new Stack<>();
while(sum!=0){
int n = sum %10;
sum = sum/10;
stack.push(str[n]);
}
while(!stack.isEmpty()){
System.out.print(stack.pop());
if(!stack.isEmpty()){
System.out.print(" ");
}
}
}
}