算法:一维最近对问题

只考虑y轴轴值为0,x轴可以变化,用分治法实现:

#include 
#include 
using namespace std;
const double Max = 201230089058;
double nearest1(double *array, double low, double high, int count);
int main() {
	int n;
	cout<<"输入x轴坐标数目:";
	cin>>n;
	double *p = new double[n];
	cout<<"输入每个x轴值:";
	for(int i = 0; i < n; i++) {
		cin>>p[i];
	}
	
	sort(p, p + n);

	cout <<"最近值为 = "<= average)) {
			break;
		}
	}

	dis = array[j + 1] - array[j];
	double leftdis;
	double rightdis;
	
	leftdis = nearest1(array, array[0], array[j], j + 1);
	rightdis = nearest1(array + j + 1,array[j + 1], array[count - 1], count - j -1);

	dis = (dis <= leftdis) ? dis : leftdis;
	dis = (dis <= rightdis) ? dis : rightdis;
	return dis;
}


你可能感兴趣的:(算法)