C++ 90 最后一个测试点WA 求调

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

znzryb @ 2024-08-01 09:58:00

#include <iostream>
#include <iomanip>
#include <math.h>

// int 有十位
int s,v,t,h,m;
using namespace std;
int main()
{
    cin >> s >> v;
            // 只有小数与整数的结果是小数
    t = ceil(static_cast<double>(s)/v)+10;
    h = floor(t/60);
    m = t % 60;
    // 对无min的进行特判
    if (m==0){
        if (h<=8){
            cout<<0<<8-h<<":"<<"00";
        }else if(h>8 and h<=20){
            cout<<24-(h-8)<<":"<<"00";
        }else{                      // 不要打两个数字00
            cout<<0<<24-(h-8)<<":"<<"00";
        }
    }else{
        if (h<=7){
            if (m<=50){
                cout<<0<<8-h-1<<":"<<60-m;
            }else{
                cout<<0<<8-h-1<<":"<<"0"<<60-m;
            }  

        }else if(h>7 and h<=19){
            if (m<=50){
                cout<<24-(h-8)-1<<":"<<60-m;
            }else{
                cout<<24-(h-8)-1<<":"<<"0"<<60-m;
            }    
        }else{
            if (m<=50){
                cout<<"0"<<24-(h-8)-1<<":"<<60-m;
            }else{
                cout<<"0"<<24-(h-8)-1<<":"<<"0"<<60-m;
            }  
        }
    }
    return 0;
}

by znzryb @ 2024-08-01 10:05:48

最后一个的边界判断出了问题,会输出011:50这种,改成这样就好了

#include <iostream>
#include <iomanip>
#include <math.h>

// int 有十位
int s,v,t,h,m;
using namespace std;
int main()
{
    cin >> s >> v;
            // 只有小数与整数的结果是小数
    t = ceil(static_cast<double>(s)/v)+10;
    h = floor(t/60);
    m = t % 60;
    // 对无min的进行特判
    if (m==0){
        if (h<=8){
            cout<<0<<8-h<<":"<<"00";
        }else if(h>8 and h<=22){
            cout<<24-(h-8)<<":"<<"00";
        }else{                      // 不要打两个数字00
            cout<<0<<24-(h-8)<<":"<<"00";
        }
    }else{
        if (h<=7){
            if (m<=50){
                cout<<0<<8-h-1<<":"<<60-m;
            }else{
                cout<<0<<8-h-1<<":"<<"0"<<60-m;
            }  

        }else if(h>7 and h<=21){
            if (m<=50){
                cout<<24-(h-8)-1<<":"<<60-m;
            }else{
                cout<<24-(h-8)-1<<":"<<"0"<<60-m;
            }    
        }else{
            if (m<=50){
                cout<<"0"<<24-(h-8)-1<<":"<<60-m;
            }else{
                cout<<"0"<<24-(h-8)-1<<":"<<"0"<<60-m;
            }  
        }
    }
    return 0;
}

by GAOchang12 @ 2024-08-07 11:21:02

@znzryb 这是顺序结构

#include <bits/stdc++.h>
using namespace std;
int main(){
    float s,u;//int类型上取整无效 
    int t,hh=7,mm=0;
    cin>>s>>u;
    t=ceil(s/u)+10;//上学时间 
    hh=hh-(t/60)+24;//加24防止负数 
    mm=mm-(t%60)+60;//加60防止负数 
    printf("%.2d:%.2d",hh%24,mm%60);//c++的一个特性,%.2d是往前补0 
    return 0;
}

by znzryb @ 2024-08-07 15:08:59

@GAOchang12 确实,牛逼


|