我们作业有一道要用C语言做一个火车旅行时间计算的题目。要求输入712和1411得到答案the train journey time is 6hrs 59mins.在下做的没有错误和警告,但是得不到正确答案。请大侠帮忙!
#include <stdio.h>
int main()
{int time1,time2,hours,mins;
scanf("%d,%d",&time1,&time2);
if (time2%100 > time1%100){
mins=time2%100-time1%100;hours=(int)(time2/100)-(int)(time1/100);}
else mins=time2%100+60-time1%100;
hours=(int)(time2/100)-(int)(time1/100)-1;
printf("the train journey time is %d hrs %d mins\n",hours,mins);
}
scanf("%d,%d",&time1,&time2)这里错了。。
scanf("%d%d",&time1,&time2)
多了个逗号。
#include <stdio.h>
int main()
{int time1,time2,hours,mins;
scanf("%d%d",&time1,&time2);
if (time2%100 > time1%100){
mins=time2%100-time1%100;hours=(int)(time2/100)-(int)(time1/100);}
else mins=time2%100+60-time1%100;
hours=(int)(time2/100)-(int)(time1/100)-1;
printf("the train journey time is %d hrs %d mins\n",hours,mins);
}
#include <stdio.h>
int main()
{int time1,time2,hours,mins;
scanf("%d,%d",&time1,&time2);
if (time2%100 > time1%100){
mins=time2%100-time1%100;
hours=(int)(time2/100)-(int)(time1/100);
}
else {mins=time2%100+60-time1%100;
hours=(int)(time2/100)-(int)(time1/100)-1;}//else的范围,加大括号
printf("the train journey time is %d hrs %d mins\n",hours,mins);
}
请详细说明一下题目,比如输入的两个数字是干什么的....没太看明白你的程序
另外hours=(int)(time2/100)-(int)(time1/100)部分
在C语言中,定义的整型变量在做除法时会自动舍去小数部分,不需要强制转换数据类型
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息