8,9,10过不去,求大佬

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

Sharkch @ 2024-01-17 13:41:49

#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{
    double s, v, t;
    cin >> s >> v;
    int H, num;
    t = s / v + 10;
    H = 7;
    num = t / 60;
    if (num)
    {
        H -= num;
        t -= 60 * num;
    }
    cout << setw(2) << setfill('0') << H << ":" << setw(2) << setfill('0') << 60 - ceil(t);
    return 0;
}

by leiaxiwo @ 2024-01-17 14:05:43

@Sharkch 参考我的

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
double s,v,m;
int n,a,t,b;
int main()
{
    s=read(),v=read();
    n=8*60+24*60;
    t=ceil(s/v)+10;
    n=n-t;
    if(n>=24*60) n-=24*60;
    b=n%60,a=n/60;
    if(a<10)
    {
        if(b<10){
            cout<<"0"<<a<<":0"<<b;
        } 
        else{
            cout<<"0"<<a<<":"<<b;
        } 
    }
    else
    {
        if(b<10){
            cout<<a<<":0"<<b;
        } 
        else{
            cout<<a<<":"<<b;
        } 
    }
    return 0;
}

by I_Love_DS @ 2024-01-17 14:08:15

审题很重要。

得考虑半夜出发的情况。

在14,15行中间添加:

if (H - sum < 0) H += 24;

就AC了。


by Sharkch @ 2024-01-17 14:12:09

#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{
    double s, v, t;
    cin >> s >> v;
    int H, num;
    t = s / v + 10;
    H = 7;
    num = t / 60;
    if (num)
    {
        if (num > 8)
        {
            H = 24 - num + 8;
            t -= 60 * num;
        }
        H -= num;
        t -= 60 * num;
    }
    cout << setw(2) << setfill('0') << H << ":" << setw(2) << setfill('0') << 60 - ceil(t);
    return 0;
}

为什么我这么加还是不行哇@liuruiqing


by I_Love_DS @ 2024-01-17 14:34:40

调不出来了……

看我的代码吧……

#include <bits/stdc++.h>
using namespace std;
int s,v;
int main(){
    cin >> s >> v;
//  scanf("%d%d",&s,&v);
    int k = ceil((double)s / v) + 10;
    int t = 480 + 1440;
    t -= k;
    if (t >= 1440) t -= 1440;
    if (t / 60 < 10)            /*printf("0");*/cout << 0;
    cout << t / 60 << ":";
//  printf("%d:",t / 60);
    if (t % 60 < 10) /*printf("0");*/cout << 0;
//  printf("%d",t % 60);
    cout << t % 60;
    return 0;
}

by Sharkch @ 2024-01-17 15:11:17

@liuruiqing 已经好了,多谢大佬


by panghuda @ 2024-01-18 20:21:53

难得现在还有人在和我一样刷这样的基础题,感觉同为算法小白我的更容易理解一点,就直接分为半夜0点之前和之后就行。让你看看我的吧:

#include<bits/stdc++.h>
using namespace std;
int main(){
    double c,v;
    int hour,min;
    cin>>c>>v;
    int minute = ceil(c/v);
    int minutes = (minute+10);
    if(minutes>480){
        hour = 23-((minutes-480)/60);
        min = 60-((minutes-480)%60);
        printf("%02d:%02d",hour,min);
    }
    else if(minutes<=480)
    {
        hour = ((480-minutes)/60);
        min = ((480-minutes)%60);
        printf("%02d:%02d",hour,min);
    }
    return 0;
}

|