7-10 Calculate train running time (15 points)
This question requires writing a program to calculate the time taken for the entire journey based on the departure time and arrival time of the train.
Input format:
Enter two 4-digit positive integers in one line, separated by spaces, representing the departure time and arrival time of the train respectively. Each time is formatted as 2-digit hours (00-23) and 2-digit minutes (00-59), assuming departure and arrival are on the same day.
Output format:
output the time spent on the journey in one line, the format is "hh:mm", where hh is 2-digit hours and mm is 2-digit minutes.
Input sample:
1201 1530
Output sample:
03:29
#include <stdio.h>
#include <stdlib.h>
int main()
{
int h; //小时
int m; //分钟
int num1; //出发时间
int num2; //到达时间
scanf("%d%d",&num1,&num2);
h=num2/100-num1/100; //4位整型取前2位(小时)
m=num2%100-num1%100; //4位整型取余后2位(分钟)
if(m<0) //num1,num2都是在同一天,h一定大于0
{
m=60+m; //当m小于0,从h借来一小时,填补m的值
h=h-1;
}
printf("%02d:%02d\n",h,m);
return 0;
}