做一道C语言程序设计的题,十万火急!已知列车出发时间(输入格式为4位数,如0825),甲乙两地距离(单位是千米),速度(km/h).求列车到底乙地的时间,要求格式为(小时:分钟
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/22 19:16:25
做一道C语言程序设计的题,十万火急!已知列车出发时间(输入格式为4位数,如0825),甲乙两地距离(单位是千米),速度(km/h).求列车到底乙地的时间,要求格式为(小时:分钟
做一道C语言程序设计的题,十万火急!
已知列车出发时间(输入格式为4位数,如0825),
甲乙两地距离(单位是千米),
速度(km/h).求列车到底乙地的时间,
要求格式为(小时:分钟
做一道C语言程序设计的题,十万火急!已知列车出发时间(输入格式为4位数,如0825),甲乙两地距离(单位是千米),速度(km/h).求列车到底乙地的时间,要求格式为(小时:分钟
# include <stdio.h>
int main(void)
{
int time, distance, speed;
int hour, minute;
printf("Please input the start time (such as 0825): ");
scanf("%d",&time);
printf("Please input the distance(km): ");
scanf("%d",&distance);
printf("Please input the speed(km/h): ");
scanf("%d",&speed);
hour=distance/speed;
minute=distance%speed*60/speed;
minute=minute+time%100;
if(minute>=60)
{
minute=minute-60;
hour++;
}
hour=hour+time/100;
printf("Time of arrival: %d:%02d\n",hour,minute);
return 0;
}
没有考虑不能整除的时候,分钟四舍五入的时候,要严谨一定应该考虑,稍微改一下就可以了
下面是考虑了四舍五入的
# include <stdio.h>int main(void)
{
int time,hour,minute;
float distance, speed,a;//a用来存放分钟的小数部分,方便四舍五入
printf("Please input the start time (such as 0825): ");
scanf("%d",&time);
printf("Please input the distance(km): ");
scanf("%d",&distance);
printf("Please input the speed(km/h): ");
scanf("%d",&speed);
hour=distance/speed;
distance-=hour*speed;
a=distance*60/speed;
minute=a;
a-=minute;
if(a>=0.5)
minute++;
minute=minute+time%100;
if(minute>=60)
{
minute=minute-60;
hour++;
}
hour=hour+time/100;
printf("Time of arrival: %d:%02d\n",hour,minute);
return 0;
}