永发信息网

c语言做火车时间求助

答案:3  悬赏:80  手机版
解决时间 2021-08-01 20:11
  • 提问者网友:王者佥
  • 2021-08-01 09:29

我们作业有一道要用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);


}

最佳答案
  • 五星知识达人网友:蕴藏春秋
  • 2021-08-01 10:40

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);



}

全部回答
  • 1楼网友:杯酒困英雄
  • 2021-08-01 11:47

#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); }

  • 2楼网友:底特律间谍
  • 2021-08-01 11:27

请详细说明一下题目,比如输入的两个数字是干什么的....没太看明白你的程序

另外hours=(int)(time2/100)-(int)(time1/100)部分

在C语言中,定义的整型变量在做除法时会自动舍去小数部分,不需要强制转换数据类型

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯