题解 | 预知

预知

https://www.nowcoder.com/practice/d9dd8fb2f4ca4385b3728b7bf9c9d0a4

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;

int a[N];

void solve() {
    int n; cin >> n;
    for (int i = 1; i <= n; i ++ ) cin >> a[i];
    if (n == 1) {
        cout << -1 << endl;
        return ;
    }
    int mx = -1;
    for (int i = 1; i <= n; i ++){
        mx = max(mx, a[i]);
    }
    if (mx == 1) cout << 0 << endl;
    else {
        int cnt = 0;
        for (int i = 1; i <= n; i ++ ) {
            if (a[i] > 1) cnt ++;
        }
        if (cnt == 1) cout << mx - 1 << endl;
        else cout << mx << endl;
    }
}

int main(){
    int T; cin >> T;
    while (T -- ) solve();
    return 0;
}

全部评论

相关推荐

双非阴暗爬行:我来看看笑死我了,这名字取得好想笑(没有不好的意思)
点赞 评论 收藏
分享
嵌入式求职之路:可以看我经验😂,https://www.nowcoder.com/share/jump/73221730841876945
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务