,计算并输出旅途时间。
【问题描述】2.2.7 输入2个整数time1和time2,表示火车的出发时间和到达时间,计算并输出旅途时间。有效的时间范围是0000 ~2359(前两位表示小时,后两位表示分钟),不需要考虑出发时间晚于到达时间的情况。【样例输入输出】(括号内为说明文字)
Enter time1: 712 (出发时间是7:12)
Enter time2: 1411 (到达时间是14:11)
The train journey time is 6 hours 59 minutes
自己设计了个程序可以实现,,但也不保证绝对能够包容所有输入,,
程序代码:
#include<stdio.h> void main() { int time1,time2,train_journey_time,m,n; printf("Enter time1: (0000<time1<2359)"); scanf("%d",&time1); while(time1/100>=24||(time1/100<=23&&time1%100>59)) { printf("Input error! please input again:"); scanf("%d",&time1); } printf("Enter time2: (0000<time2<2359)"); scanf("%d",&time2); while(time2/100>=24||(time2/100<=23&&time2%100>59)) { printf("Input error! please input again:"); scanf("%d",&time2); } if(time1%100>time2%100) time2=(time2/100-1)*100+(time2%100+60); train_journey_time=time2-time1; m=train_journey_time/100;n=train_journey_time%100; printf("The train journey time is %d hours %d minutes!\n",m,n); },代码如下,,,拿出来仅供大家交流,有好多方法,互相学习、