L1-007 念数字
问题描述
代码
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int n;
string s[10] = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };
string str;
cin >> n;
if (n == 0) {
cout << "ling";
return 0;
}
stringstream newstr;
newstr << n;
str = newstr.str();
if (str[0] == '-') {
cout << "fu ";
str.erase(0,1);
}
for (int i = 0; i < str.length(); i++) {
if (str[i] == '0') {
str.erase(i,1);
} else {
break;
}
}
for (int i = 0; i < str.length(); i++) {
cout << s[str[i]-'0'];
if (i != str.length()-1)
cout << " ";
}
return 0;
}
代码思路
输入一个整数,判断是否为零,是就直接输出零结束,不是就将该整数转换为字符串类型,做字符串的操作。