#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
int main(void){
int n,k=0,avg;
vector<int> v,g;
set<int> s;
cin >> n;
pair<int,int> p[n];
s.clear();
while(k<n){
cin >> p[k].first >> p[k].second; //first==x second==y
v.push_back(p[k].first);//存x
k++;
}
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++){ //control x=v[i]
g.clear();
for(int j=0;j<n;j++){
if(v[i]==p[j].first){
g.push_back(p[j].second);
}
}if(g.size()==1){
avg=g[0];
}else{
sort(g.begin(),g.end());
avg=g[0]+g[g.size()-1]; //找y
}
s.insert(avg); //確認每項x variable 的y值
if(g.size()!=1){
if((g.size()%2)==0){
for(int j=0;j<g.size()/2;j++){
if(avg!=((g[j]+g[g.size()-(1+j)])/2)){
cout << "failure" << endl; return 0;
}
}
}else{
for(int j=0;j<g.size()/2;j++){
if((avg!=((g[j]+g[g.size()-(1+j)])/2))||(g[g.size()/2] != avg)){
cout << "failure" << endl; return 0;
}
}
}
}else continue;
}
if(s.size()>1)
cout << "failure" << endl;
else
cout << "success" << endl;
return 0;
}
以解決
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
int main(void){
int n,k=0;
float sum,avg;
vector<int> v,v1;
set<int> s;
cin >> n;
pair<int,int> p[n];
while(k<n){
cin >> p[k].first >> p[k].second;
v.push_back(p[k].first);
k++;
}
sort(v.begin(),v.end()); //x範圍
for(int i=0;i<v.size();i++){
sum=0;
v1.clear();
for(int j=0;j<n;j++){
if(v[i]==p[j].first){
v1.push_back(p[j].second);
}
}sort(v1.begin(),v1.end());
avg=(v1[0]+v1[v1.size()-1])/2*1.0;
for(int l=0;l<v1.size();l++){
sum+=v1[l];
} sum/=(v1.size())*1.0;
if(sum != avg ){
cout << "failure" << endl; return 0;
}
s.insert(sum);
}
if(s.size()==1){
cout << "success" << endl;
}else cout << "failure" << endl;
return 0;
}