学英语

学英语_第1张图片

知识点:字符串,控制流(条件控制)。

思路:从本质上分析问题,从人思考问题的角度出发。需对不同大小的数字按不同的情况分类,给予不同的处理方法。

#include 
#include 
using namespace std;
string s1[20] = { "one", "two" , "three", "four", "five" , "six", "seven", "eight" , "nine","ten" ,"eleven", "twelve", "thirteen" , "fourteen", "fifteen","sixteen" ,"seventeen", "eighteen", "nineteen" };
//string s2[10] = {"ten","eleven","twelve", "thirteen", "fourteen","fifteen","sixteen","seventeen", "eighteen", "nineteen"};
string s3[10] ={ "twenty", "thirty" , "forty", "fifty", "sixty" , "seventy", "eighty", "ninety" };
string lessThan100( long num)
{
	string result;
	if(num < 20)
		result = s1[num-1];
	else if (num <100 && num%10 == 0 )
		result = s3[num/10-2];
	else if (num <100)
		result = s3[num/10-2]+ " "+s1[num%10-1];
	return result;
}
string lessThan1000( long num)
{
	string result;
	if(num < 100)
		result = lessThan100(num);
	else if(num%100 == 0)
		result = s1[num/100-1] + " hundred";
	else
		result = (s1[num/100 -1]+ " hundred and "+ lessThan100(num%100));
	return result;
}
string lessThanMillion( long num)
{
	string result;
	if(num < 1000)
		result = lessThan1000(num);
	else if(num % 1000 == 0)
		result = lessThan1000(num/1000) + " thousand";
	else
		result = lessThan1000(num/1000) + " thousand "+ lessThan1000(num%1000);
	return result;
}
string lessThanBillion( long num)
{
	 string result;
	 if(num <1000000)
		result = lessThanMillion(num);
	 else if(num % 1000000 ==0)
		 result = lessThanMillion(num/1000000) + " million";
	 else
		 result = lessThanMillion(num/1000000) + " million " + lessThanMillion(num%1000000);
	return result;
}
string parse(long num)
{
	string result;
	if(num <= 0 || num >=10000000000)
		result = "ERROR";
	else if(num < 1000)
		result = lessThan1000(num);
	else if(num < 1000000)
		result = lessThanMillion(num);
	else if(num <1000000000)
		result = lessThanBillion(num);
	else if (num == 1000000000)
		result = "one billion";
	else
		result = s1[num/1000000000-1]+ " billion "+ lessThanBillion(num%1000000000);
	return result;
}
int main()
{
	long num =0 ;
	cin >> num;
	cout << parse(num);
	return 0;
}



你可能感兴趣的:(华为OJ初级题)