#include "iostream"
#include "cmath"
#include "fstream"
using namespace std;
//在元素个数为n的数组a中寻找第k小的元素
//如果成功,返回第k小的元素,否则返回0
int select(int a[], int n, int k)
{
int low = 1;
int heigh = n;
while(low <= heigh)
{
int mid = (low + heigh) / 2;
if(a[mid] == a[k])
return a[k];
else if(a[mid] > a[k])
heigh = mid - 1;
else
low = mid + 1;
}
return 0;
}
//返回元素个数为n的数组a中的每一个元素到yk的距离之和
int dis(int a[], int n, int yk)
{
int sum = 0;
for(int i=1; i<=n; i++)
sum += abs(a[i]-yk);
return sum;
}
int main()
{
int n;
ifstream fin("gap.txt");
fin >> n;
int *x, *y;
x = new int[n+1];
y = new int[n+1];
for(int i=1; i<=n; i++)
fin >> x[i] >> y[i];
cout << "油井个数为:" << n << endl;
cout << "坐标分别为:\n";
for(i=1; i<=n; i++)
cout << x[i] << " " << y[i] << endl;
int yk = select(y, n, n/2);
int min = dis(y, n, yk);
cout << "最小距离为:" << min << endl;
return 0;
}
输油管道问题
最新推荐文章于 2021-04-20 11:09:22 发布