求大佬帮助 互关·

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

polyoid @ 2024-12-11 21:46:21

#include <iostream>
#include<cmath>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    int s,v;
    cin>>s>>v;
    int t=ceil(s/v);
    t+=10;
    int maxh=0, maxmin=0; 
    int h=0,m=0;
    for(int i=0;i<24;i++){
        for(int j=0;j<60;j++){
            if(i*60+j+t<480){
                if(i>maxh&&j>maxmin){
                        h=i;
                        m=j;
                }

        }
            }
        }

    if(h<=9){
        cout<<"0"<<h<<":";
    }
    else{
        cout<<h<<":";
    }
    if(m<=9){
        cout<<"0"<<m<<endl;
    }
    else{
        cout<<m<<endl;
    }
    return 0;
}

感觉我的代码太复杂了,不知道该怎么优化。。。


by FYR2013 @ 2024-12-11 21:48:42

#include<bits/stdc++.h>

using namespace std;

int s,v;
bool flag;
int ans;
int ans_h;
int ans_min;

int main(){
    cin>>s>>v;
    if(s / v + 10 > 480)flag = true;
    if(!flag){
        if(s % v == 0)ans = 480 - s / v - 10;
        else ans = 480 - s / v - 10 - 1;
        int tmp = ans;
        while(1){
            if(tmp < 60)break;
            tmp -= 60,ans_h ++;
        }
        ans_min = tmp;
        if(ans_h < 10 && ans_min < 10)cout<<0<<ans_h<<":"<<0<<ans_min;
        if(ans_h < 10 && ans_min >= 10)cout<<0<<ans_h<<":"<<ans_min;
        if(ans_h >= 10 && ans_min < 10)cout<<ans_h<<":0"<<ans_min;
        if(ans_h >= 10 && ans_min >= 10)cout<<ans_h<<":"<<ans_min;
        exit(0);
    }
    else {
        if(s % v == 0)ans = s / v + 10;
        else ans = s / v + 10 + 1;
        int tmp = ans;
        tmp -= 480;
        ans_h = 23;
        ans_min = 60;
        while(1){
            if(tmp < 60)break;
            tmp -= 60,ans_h --;
        }
        ans_min = 60 - tmp;
        if(ans_h < 10 && ans_min < 10)cout<<0<<ans_h<<":"<<0<<ans_min;
        if(ans_h < 10 && ans_min >= 10)cout<<0<<ans_h<<":"<<ans_min;
        if(ans_h >= 10 && ans_min < 10)cout<<ans_h<<":0"<<ans_min;
        if(ans_h >= 10 && ans_min >= 10)cout<<ans_h<<":"<<ans_min;
        exit(0);
    }
}

by polyoid @ 2024-12-11 22:03:34

@FYR2013 感谢大佬,已关


|