要記得考慮找不到公差的情況,就代表缺少的數字一定在中間(如我只有五項的話)
解法:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
using namespace std;
int main() {
int t;
cin >> t;
string s;
getline(cin ,s);
vector<int>v;
while(t--){
getline(cin ,s);
int n,m,d,dd;
istringstream iss(s);
while(iss >> n){
v.push_back(n);
}
sort(v.begin(),v.end());
bool found=false;
for(int i=0;i<v.size();i++){
if(v[i]==v[i+1]){
d=v[i];
v[i]=v[i-1];
}
else if((v[i-1]+v[i+1])/2 == v[i]){
m = v[i]-v[i-1];//公差
for(int i=0;i<v.size();i++){
if(v[i+1] - v[i] == m*2){
m=v[i+1]-m;
}
}
found=true;
}
}
if(!found){
int g=(v[0]+v[v.size()-1])/2;
cout << g <<' ';
}
else{
cout << m << ' ';
}
cout <<d <<'\n';
v.clear();
}
}