1002 写出这个数 (20 分)

题目链接

  1. 题目的意思很明确,将一个小于10的100次方的数的各个位数相加,然后将加和得到的值得每一位按拼音输出。
  2. 输入的数字要用字符串存储,否则会溢出。

c++代码

#include<iostream>
#include<string>
#include<vector>
#include <algorithm> 
using namespace std;

int main() {
	string s;
	cin >> s;
	string result[] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
	long int sum = 0;
	for (int i = 0; i < s.size(); i++) {
		sum += s[i] - '0';
	}
	string res = to_string(sum);
	for (int i = 0; i < res.size(); i++) {
		if (i != 0)
			cout << " ";
		cout << result[res[i] - '0'];
	}
	cout << endl;
}
复制代码

python代码

result = [ "ling","yi","er","san","si","wu","liu","qi","ba","jiu" ]

sum = 0
s = input()
for a in s:
    sum += int(a)

sum = str(sum)
for i,ch in enumerate(sum):
    if i!=0:
        print(" ",end='')
    print(result[int(ch)],end='')
复制代码

转载于:https://juejin.im/post/5ce2c9cef265da1bb96fb043

发布了177 篇原创文章 · 获赞 44 · 访问量 18万+
展开阅读全文
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览