请问使用循环为什么WA #4,#5,#6,程序逻辑哪里错了?谢谢。
查看原帖
请问使用循环为什么WA #4,#5,#6,程序逻辑哪里错了?谢谢。
787499
killea楼主2022/9/14 09:52

超时可以接受,现在WA真是看不懂了。程序逻辑错了嘛?我就想用循环解这个题。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
    long long n,m;
    long long res = m;
    vector<long long> v;
    cin >> n >> m;
    for(long long i = 0; i < n; i++){
        long long a;
        cin >> a;
        v.push_back(a);
    }
    for(long long h = *max_element(v.begin(), v.end()) ; h > 0; h--){
        res = m ;
        for(auto tree : v){
            if(tree > h ){
            res = res - (tree - h);
            }
        }
        if(res == 0){
            cout << h << endl;
            return 0;
        }
    }
}
2022/9/14 09:52
加载中...