【样例1】
6
1 4 4 7 4 1
【样例2】
4
1 3 3 7
【样例1】
3
【样例2】
-1
//终于成功了 #include <cmath> #include <iostream> #include <algorithm> using namespace std; const int N = 110; int n; int a[N], b[N]; int main(){ cin >> n; for (int i = 1; i <= n; i ++ ) cin >> a[i]; sort(a + 1, a + 1 + n); for (int i = 1; i <= n; i ++ ) { b[a[i]] ++; } int t = 0; int d[N]; for (int i = 1; i <= 101; i ++ ){ if (b[i] != 0) d[++ t] = i; } if (t == 1) cout << 0 << endl; else if (t == 2) cout << d[2] - d[1] << endl; else if (t == 3) { if (d[1] + d[3] == 2 * d[2]) cout << d[3] - d[2] << endl; else cout << "-1" << endl; } else cout << "-1" << endl; return 0; }
//不知道那里错了,但是只有八十分,有哪位大神可以指出来吗 #include <cmath> #include <iostream> using namespace std; const int N = 110; int n; int a[N]; int main(){ cin >> n; for (int i = 0; i < n; i ++ ) cin >> a[i]; int minn = 111, maxn = 0; for (int i = 0; i < n; i ++ ){ minn = min(minn, a[i]); maxn = max(maxn, a[i]); } int x = maxn + minn >> 1; int y = maxn - x; bool t = true; for (int i = 0; i < n; i ++ ){ if (abs(a[i] - x) != 0 && abs(a[i] - x) != y) t = false; } if (t) cout << y << endl; else cout << "-1" << endl; return 0; }
原文:https://www.cnblogs.com/Iamcookieandyou/p/13110262.html