题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1727
题目翻译:
把数字转换为英文并输出,简单模拟。
AC代码:
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
string s[] = {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
string shiwei[] = {"0","1","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
///如果数字是0~19,使用第一个函数
int main() {
int number;
while(~scanf("%d",&number)) {
string ans = "";
if(number>=0 && number<20) { ///如果数字四0~20则直接输出
ans += s[number];
}
else if(number>=20 && number<100) { ///如果是20~100
ans += shiwei[number/10];
number = number%10;
if(number) {
ans += "-" + s[number];
}
}else if(number>=100 && number<1000) {
ans = s[number/100] + " hundred";
number = number%100;
if(number) {
if(number>=0 && number<20) {
ans += " and " + s[number];
}
else {
ans += " and " + shiwei[number/10];
number = number%10;
if(number) {
ans += "-" + s[number];
}
}
}
}
else { ///是四位数
ans += s[number/1000] + " thousand";
number = number%1000;
if(number) {
if(number>=0 && number<20) {
ans += " and " + s[number];
}
else if(number>=20 && number<100) {
ans += " and " + shiwei[number/10];
number = number%10;
if(number) {
ans += "-" + s[number];
}
}else {
ans += " and " + s[number/100] + " hundred";
number = number%100;
if(number) {
if(number>=0 && number<20) {
ans += " and " + s[number];
}
else if(number>=20 && number<100) {
ans += " and " + shiwei[number/10];
number = number%10;
if(number) {
ans += "-" + s[number];
}
}
}
}
}
}
cout<<ans<<endl;
}
return 0;
}