题解 | #学英语#
学英语
http://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
这道题的思路就是三个三个的处理,根据题目要求实现就行了
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
vector<string> v = {
"and",
"hundred",
"thousand",
"million",
"billion"
};
map<int,string> mtwenty = {
{1,"one"},{2,"two"},{3,"three"},{4,"four"},{5,"five"},{6,"six"},{7,"seven"},
{8,"eight"},{9,"nine"},{10,"ten"},{11,"eleven"},{12,"twelve"},{13,"thirteen"},
{14,"fourteen"},{15,"fifteen"},{16,"sixteen"},{17,"seventeen"},
{18,"eighteen"},{19,"nineteen"}
};
map<int,string> mhundred = {
{2,"twenty"},{3,"thirty"},{4,"forty"},{5,"fifty"},{6,"sixty"},
{7,"seventy"},{8,"eighty"},{9,"ninety"}
};
void mytransform(long long x,int cnt,string &out){
string space = " ";
string temp;
if(x<20) temp = mtwenty[x];
else if(x<100){
int hundred = x/10;
int r1 = x%10;
if(r1==0)temp = mhundred[hundred];
else temp = mhundred[hundred]+' '+mtwenty[r1];
}
else if(x<1000){
int thousand = x/100;
int r1 = x%100;
int hundred = r1/10;
int r2 = r1%10;
if(r1==0) temp = mtwenty[thousand]+space+v[1];
else if(hundred==0)temp = mtwenty[thousand]+space+v[1]+
" and "+mtwenty[r2];
else if(r2==0) {
if(hundred>1) temp = mtwenty[thousand]+space+v[1]+
" and "+mhundred[hundred];
else temp = mtwenty[thousand]+space+v[1]+" and "+mtwenty[r1];
}
else {
if(r1<20)temp = mtwenty[thousand]+space+v[1]+" and "+mtwenty[r1];
else temp = mtwenty[thousand]+space+v[1]+" and "+
mhundred[hundred]+' '+mtwenty[r2];
}
}
if(cnt>1)out = temp+space+v[cnt]+space+out;
else out = temp+space+out;
}
int main() {
long long n;
while(cin>>n){
long long &q = n,r = 0;
string out;
int cnt = 1;
while(q){
r = q%1000;
q = q/1000;
mytransform(r, cnt,out);
cnt++;
}
cout<<out<<endl;
}
}