水题,不解释,直接上代码。
#include<bits/stdc++.h> using namespace std; int a[100010]; int cmp(int a, int b) { return a > b; } int main() { int n; while(scanf("%d", &n) != EOF) { for(int i = 0;i < n;i++) { scanf("%d", &a[i]); } if(n<3) {printf("-1\n");continue;} sort(a, a + n, cmp); int flag = 0; for(int i = 2;i < n;i++) { if(a[i-2] + a[i-1] > a[i] && a[i-1] + a[i] > a[i-2] && a[i] + a[i-2] > a[i-1]){flag = a[i-2] + a[i-1] + a[i];break;} } if(flag) printf("%d\n", flag); else printf("-1\n"); } return 0; }