1295 爱翻译

题目描述

英语是现在世界第一大语言,所以学好英语是very important,
但是宁波大学的某个大牛说:“我不怕,我可以写一个程序让各种语言
快速转换。”哎呀,学编程的,惹不起…溜…┈━═☆
咱不是大牛,但有追逐大牛的梦想,大牛的伟大程序不是随便能盗版出来的,
我们就做一个简单的翻译器,把阿拉伯数字转换为英文输出。

输入要求

小于10000的非负阿拉伯数字整数n。

输出要求

对应的英文,如样例,一个一行。

测试数据

//输入
3923
5021

//输出
three thousand nine hundred twenty three
five thousand twenty one

AC代码

折磨人的代码,多棒 = =

考虑好10-19,11是eleven,不是ten one
注意输入0 也要输出zero,而不是什么都不输出
其他照旧

#include <bits/stdc++.h>
using namespace std;
int main() {
	string in,res[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
	string shi[10]={"","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
	string shi2[10]={"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
	int f,num;
	while(cin>>in)
	{
		if(in.size()==1) //个位数单独考虑 
		{
			cout<<res[in[0]-'0']<<endl;continue;
		}
		f=0;
		// 输出千位和百位,如果没有则不用输出 
		for(int i=0;i<in.size()-2;i++) 
		{
			if(in[i]=='0') continue;
			if(f) cout<<" ";f=1;
			cout<<res[in[i]-'0'];
			if(in.size()-i==4) printf(" thousand");
			if(in.size()-i==3) printf(" hundred");
		}
		
		 // 输出十位和个位
		num=(in[in.size()-2]-'0')*10+in[in.size()-1]-'0';
		if(num>=10 && num<=19)
		{
			if(f) cout<<" ";f=1;
			cout<<shi2[num-10];
		}
		else 
		{
			for(int i=in.size()-2;i<in.size();i++) 
			{
				if(in[i]=='0') continue;
				if(f) cout<<" ";f=1;
				if(in.size()-i==2) cout<<shi[in[i]-'0'];		
     			else cout<<res[in[i]-'0'];
			}
		}
		cout<<endl;
	}
	return 0;
}