题目:
背景:钟面上的时针和分针之间的夹角总是在 0 度~ 359 度之间。举例来说,在十二点的时候两针之间的夹角为 0 度,而在六点的时候夹角为 180 度,在三点的时候为 90 度。本题要解决的是计算 0:00 到 12:00之间任意一个时间的夹角。
输入:要求输入必须为 小时h分钟m的格式,如12h00m, 5h43m等,其他形式的输入都不接收,如 12,00 或5h43等输入都不被接收,而且需要对“小时”和“分钟”进行数值有效判断(小时在[0,12],分钟在[0.60]之间),不满足以上要求时,需要重新输入。
在程序中首先打印:Please input time(e.g: 5h43m),然后输入时间。
输出:对应每组测试数据,用常用格式显示时间以及这个时候时针和分针间的最小夹角,精确到小数点后一位。如:
输入“12h00m”时,输出 At 12:00 the angle is 0.0 degrees.
输入“5h43m” 时,输出 At 5:43 the angle is 86.5 degrees.
输入“5h5m” 时,输出 At 5:05 the angle is 122.5 degrees.
提示1:以表中心到12点的连线为基准,分针每走1分钟是6度,时针与基准的夹角每个小时也是30度,从整点开始,每过1分钟时针再增加0.5度。要求结果角度为正值,即最终要取绝对值(fabs函数)
提示2:二者之间角度不应大于180度,如果大于,应用360度减去该角度。
输入提示:“Please input time(e.g: 5h43m)\n”
输入格式:"%d%c%d%c"
输出格式:“At %d:%02d the angle is %.1f degrees.\n”
程序运行示例:
Please input time(e.g: 5h43m)
7h56m
At 7:56 the angle is 98.0 degrees.
代码:
#include <stdio.h>
#include <math.h>
int main()
{
int h,m,i=0;
float alpha=0;
printf("Please input time(e.g: 5h43m)\n");
scanf("%dh%dm",&h,&m);
for(;i==0;)
{
if(h<12 && h>0 && m>0 && m<60)
{ i=1;
alpha=fabs(30*h-5.5*m);
if(m<10)
printf("At %d:0%d the angle is %.1f degrees.",h,m,alpha);
else
printf("At %d:%d the angle is %.1f degrees.",h,m,alpha);
}
else
{
printf("Please input time(e.g: 5h43m)");
scanf("%dh%dm",&h,&m);
}
}
}