[C]70分求助,实在不知道怎么改了

P5707 【深基2.例12】上学迟到

Sakura_moyu @ 2024-11-14 20:37:44


#include <math.h>
int main(){
    int s,v;
    int walk_time,reach_time=8*60-10;
    int origin_time_m,origin_time_h;
    scanf("%d %d",&s,&v);
    walk_time=s/v;  
    }
    if(s%v!=0){
        walk_time+=1;
    }
    origin_time_m=abs(reach_time-walk_time);
    origin_time_h=origin_time_m/60;
    if (origin_time_h>8){
        origin_time_h%=24;
        origin_time_h=24-origin_time_h;
    }
    origin_time_m%=60;
    printf("%.2d:%.2d",origin_time_h,origin_time_m);

    return 0;
}

by YangCheny @ 2024-11-16 18:53:10

注意一下变量,是u。


by Sakura_moyu @ 2024-11-16 19:41:46

非常抱歉,多打了个括号,应该是这个


#include <math.h>
int main(){
    int s,v;
    int walk_time,reach_time=8*60-10;
    int origin_time_m,origin_time_h;
    scanf("%d %d",&s,&v);
    walk_time=s/v;
    if(s%v!=0){
        walk_time+=1;
    }
    origin_time_m=abs(reach_time-walk_time);
    origin_time_h=origin_time_m/60;
    if (origin_time_h>8){
        origin_time_h%=24;
        origin_time_h=24-origin_time_h;
    }
    origin_time_m%=60;
    printf("%.2d:%.2d",origin_time_h,origin_time_m);
    return 0;
}```

by Sakura_moyu @ 2024-11-16 20:53:38

改成这个就好了,不应该在没判断是不是前一天的情况下就取绝对值的,会遗漏条件。应该以一天为周期,h为正就不做更改,h为负就加一天(最多也就提前一天)


int main(){
    int s,v;
    int walk_time,reach_time=8*60;
    int origin_time_m,origin_time_h;
    //声明速度、时间,走路花的时间、要求到的时间(分钟), 出发时间(m和h) 
    scanf("%d %d",&s,&v);
    walk_time=s/v;
    if(s%v!=0){
        walk_time+=1;
    }
    //判断走路中间花费的时间,截断小数部分会迟到,所以要进一 
    origin_time_m=reach_time-walk_time-10;//计算得到的出发时间(m) 
    origin_time_h=origin_time_m/60;//换算出发时间(h) 
    origin_time_m%=60;//计算当天的时间(m) 
    if (origin_time_h<0){
        origin_time_h+=24;
    }
    if (origin_time_m<0){
        origin_time_m+=60;
        origin_time_h-=1;
    }
    printf("%.2d:%.2d",origin_time_h,origin_time_m);//得到最后时间 
    return 0;
}

|