c++ 求助

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

尝试不用循环重构代码 @[作业太多啦](/user/382416)
by XKqwq @ 2022-06-08 14:41:08


```cpp for(int i=1; ;i++) { if(a>=60) { hh-=1; mm+=60; } else { mm-=a; break; } } ``` 这一部分中,当 $a>60$ 时无法跳出循环,当然会 TLE 了 @[作业太多啦](/user/382416)
by SegTree @ 2022-06-08 15:31:35


我发现了一个致命错误 改正一下上面的代码 ``` #include<bits/stdc++.h> using namespace std; int s,v,hh=7,mm=49; int main() { cin>>s>>v; int a=s/v; for(int i=1; ;i++) { if(a>=60) { hh-=1; mm+=60; } else { mm-=a; break; } } if(hh<=9 && mm<=9) { cout<<0<<hh<<":"<<0<<mm; } if(hh<=9 || mm<=9) { if(hh<=9) cout<<0<<hh<<":"<<mm; if(mm<=9) cout<<hh<<":"<<0<<mm; } return 0; } ```
by 作业太多啦 @ 2022-06-08 17:07:16


@[jacobigSB](/user/572558) ``` #include<bits/stdc++.h> using namespace std; int s,v,hh,b=470,mm,a; int main() { cin>>s>>v; if(s%v==0) a=s/v; else a=s/v+1; if(a>b) b+=1440; b=b-a; hh=b/60; mm=b%60; if(hh<=9 && mm<=9) { cout<<0<<hh<<":"<<0<<mm; } if(hh<=9 || mm<=9) { if(hh<=9) cout<<0<<hh<<":"<<mm; if(mm<=9) cout<<hh<<":"<<0<<mm; } return 0; } ``` 重构之后 50分
by 作业太多啦 @ 2022-06-08 17:09:01


@[jpb_Saturn](/user/678965) 谢谢指出
by 作业太多啦 @ 2022-06-08 17:10:58


``` #include<bits/stdc++.h> using namespace std; double s,v; int total; int consumption; int t; int h,m; int main() { cin>>s>>v; total = 8 * 60 + 24 * 60; //总共分钟数 consumption = ceil(s / v) + 10; //上学路上花费的分钟数 t = total - consumption; //从前一天的0时0分到出发时刻所经过的时间 if(t >= 24 * 60) t -= 24 * 60; h = t / 60;//出发时 m = t % 60;//出发分 printf("%02d:%02d\n", h, m); //按格式要求输出 return 0; } ``` ~~不用谢~~
by todayfinish @ 2022-06-15 13:11:27


|