计算时间差有两种,一种是把时间都转化为分钟数,一种是把时间都转化为小时,后者是会用到除法所以不可避免产生浮点数,所以我们选择转化为分钟数来计算。
//题目:给定两个时间点计算它们的时间差,比如,1:50 和3:21
//定义四个变量来保存输入的时间
//默认a时间小于b时间
int aH=0;
int aM=0;
int bH=0 ;
int bM=0;
int diff = 0;
int diffH=0;
int diffM = 0;
scanf_s("%d %d",&aH,&aM);
scanf_s("%d %d",&bH,&bM);
//把两个时间都转化为分钟表示
aM = aM + aH * 60;
bM = bM + bH * 60;
diff = bM - aM;
diffH = diff / 60;
diffM = diff % 60;
printf("the time difference is %dh:%dmin",diffH,diffM);
原文地址:https://www.cnblogs.com/yfish/p/9600986.html
时间: 2024-10-18 22:28:47