NBUOJ 1844 C(模拟)

题目描述

写一个程序把一个用 hour:minute 表示的美国时间转换成美式英语表示的时间(按下面的格式)。
这里是转换的规则:( 注意他们可能不同于你习惯的英国规则)
第一个字符以大写字母写输出
复合的英文数目是带有连字符号的,举例来说:
forty-four
用[x_in_english] o’clock 来表示 x:00
用Quarter past [x_in_english] 来表示 x:15
用[x_in_english] thirty 来表示 x:30
用Quarter to [next_hour_in_english] 来表示 x:45
以别的方式来表示其它x:nn
[x_in_english] [nn_in_english] 当 nn<45
[60-nn_in_english] to [next_hour_in_english] 当 nn>45

Examples:
5:00 Five o’clock
10:10 Ten ten
9:22 Nine twenty-two
5:15 Quarter past five
2:30 Two thirty
6:40 Six forty
5:45 Quarter to six
8:47 Thirteen to nine
12:47 Thirteen to one (American time: 1:00 follows 12:00)

输入要求

单独的一行包括一个以hour:minutes表示的时间。
每一个hour属于[1…12],minutes总是成表示成两位在[0…59]的范围里。

输出要求

单独的一行包括一个被表示成英文的时间。

输入样例

5:45

输出样例

Quarter to six

知识点

时间转换模拟

关键点

细读题目,分析总结出题目所有需求

易错点

1.只有每行的第一个单词的第一个单词才需要大写,其余均小写
2.12点的下一个时刻是1点
3.分钟数大于45时,时刻在原来的基础上+1,采用几点差几分的表示法,如8:53,表示为seven to nine(九点差七分)。

源码

#include
#include
#include
#include
using namespace std;
int main()
{
	char h[21][10]={//从1到19的英文数字(首字母大写)
		"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Seventeen","Eighteen","Nineteen"
	};
	char h4[21][10]=//从1到19的英文数字(首字母小写)
	{
		"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","seventeen","eighteen","nineteen"
	};
	char h2[7][10]={//10,20,30,40,50的英文数字(首字母小写)
		"","ten","twenty","thirty","forty","fifty"
	};
	char h3[7][10]={//10,20,30,40,50的英文数字(首字母大写)
		"","Ten","Twenty","Thirty","Forty","Fifty"
	};
	int hour,minute,tp1,tp2;
	while(scanf("%d:%d",&hour,&minute)!=EOF)
	{
		if(minute == 0) printf("%s o'clock\n",h[hour]);  //先处理几种特殊情况,下同
		else if(minute == 30) printf("%s thirty\n",h[hour]);
		else if(minute == 15) printf("Quarter past %s\n",h4[hour]);
		else if(minute == 45) {if(hour==12) hour=0;printf("Quarter to %s\n",h4[hour+1]);}
		else if(minute<45)  //小于45的普遍情况
		{
			tp1=minute%10;
			tp2=minute/10;
			if(tp1==0) printf("%s %s\n",h[hour],h2[tp2]);
			else if(tp2<=1) printf("%s %s\n",h[hour],h4[minute]);
			else if(tp2>1) printf("%s %s-%s\n",h[hour],h2[tp2],h4[tp1]);
		}
		else if(minute>45) //大于45的普遍情况
		{
			tp1=minute%10;
			tp2=minute/10;
			if(hour==12) hour=0;
			printf("%s to %s\n",h[60-minute],h4[hour+1]);
		}
	}
	return 0;
}

你可能感兴趣的:(C语言)