上题目:
输入的n很大,这里我考虑用字符串读取,然后遍历字符串中每一个字符,在遍历的过程正好可以把数字之和求出来。求出来之后应当是一个整型变量。
这时我想到的是把这个整型变量传入一个字符流里面,然后再定义一个字符串来取字符流里面的字符,最后遍历字符串,输入拼音
思路暂时是这样,肯定还会又更简洁的思路的!
话不多说,上代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
using namespace std;
int main()
{
//输入
string str1;
cin >> str1;
int sum = 0;
//计算总和
for (int i = 0; i < str1.length(); ++i) {
sum += str1[i] - '0';
}
//将sum输入到字符流中
stringstream ss;
ss << sum;
//取字符流中的字符
string str2 = ss.str();
//输出拼音,会很长(我用if-else if)
//由于我每个拼音后面都跟了空格,因此可能会出错
//果然出错了,那怎么把最后的空格去掉呢?
//最后再添加空格就好了,如果是i==str2.length()-1,那不输出空格
for (int i = 0; i < str2.length(); ++i) {
if (str2[i] == '0') {
cout << "ling";
}
else if (str2[i] == '1') {
cout << "yi";
}
else if (str2[i] == '2') {
cout << "er";
}
else if (str2[i] == '3') {
cout << "san";
}
else if (str2[i] == '4') {
cout << "si";
}
else if (str2[i] == '5') {
cout << "wu";
}
else if (str2[i] == '6') {
cout << "liu";
}
else if (str2[i] == '7') {
cout << "qi";
}
else if (str2[i] == '8') {
cout << "ba";
}
else if (str2[i] == '9') {
cout << "jiu";
}
//输出空格
if (i != str2.length() - 1) {
cout << " ";
}
}
return 0;
}
不说了,不说了,干饭干饭!!!🍞