题解 | #学英语#
学英语
http://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
void getResult(vector<string> &result, int &num);
int main() {
long num;
cin >> num;
vector<string> result;
int numMillion = num / 1000000;
if (numMillion > 0) {
getResult(result, numMillion);
result.push_back("million");
}
int numThousand = num % 1000000 / 1000;
if (numThousand > 0) {
getResult(result, numThousand);
result.push_back("thousand");
}
int numOne = num % 1000000 % 1000;
if (numOne > 0) {
getResult(result, numOne);
}
for (auto s : result) {
cout << s << " ";
}
cout << endl;
return 0;
}
void getResult(vector<string> &result, int &num) {
map<int, string> u10 = {
{1, "one"},
{2, "two"},
{3, "three"},
{4, "four"},
{5, "five"},
{6, "six"},
{7, "seven"},
{8, "eight"},
{9, "nine"}
};
map<int, string> u20 = {
{10, "ten"},
{11, "eleven"},
{12, "twelve"},
{13, "thirteen"},
{14, "fourteen"},
{15, "fifteen"},
{16, "sixteen"},
{17, "seventeen"},
{18, "eighteen"},
{19, "nineteen"}
};
map<int, string> u100 = {
{2, "twenty"},
{3, "thirty"},
{4, "forty"},
{5, "fifty"},
{6, "sixty"},
{7, "seventy"},
{8, "eighty"},
{9, "ninety"}
};
bool hundredSign = false;
bool tenSign = false;
if (num >= 100) {
hundredSign = true;
result.push_back(u10[num / 100]);
result.push_back("hundred");
num %= 100;
}
if (num >= 10) {
tenSign = true;
if (hundredSign) {
result.push_back("and");
}
if (num <= 19) {
result.push_back(u20[num]);
num = 0;
}
else {
result.push_back(u100[num / 10]);
num %= 10;
}
}
if (num > 0) {
if (hundredSign && !tenSign) {
result.push_back("and");
}
result.push_back(u10[num]);
}
}
阿里云成长空间 763人发布