最近对问题求解代码

#include
#define N 10

double ClosetPoints(int p[N][2], int &ind1, int &ind2){
    double mindis = 1e30;
    double dis = 0;
    ind1 = 0;
    ind2 = 0;
    for(int i = 0; i < N - 2; i++){
        for(int j = i + 1; j <= N - 1; j++){
            dis = (p[i][0] - p[j][0])*(p[i][0] - p[j][0]) + (p[i][1] - p[j][1])*(p[i][1] - p[j][1]);
            if(dis < mindis){
                mindis = dis;
                ind1 = i;
                ind2 = j;
            }
        }
    }
    return mindis;
}

int main(){
    
    int p[N][2], ind1, ind2;
    double mindis;
    printf("请输入%d个点:\n", N);
    for(int i = 0; i < N; i++)
        scanf("%d %d", &p[i][0], &p[i][1]);
        
    mindis = ClosetPoints(p, ind1, ind2);
    printf("最近对为:(%d, %d)和(%d, %d)\n", p[ind1][0], p[ind1][1], p[ind2][0], p[ind2][1]);
    printf("最近距离为:%lf", mindis);

    return 0;
}
TIM截图20190310165309.jpg

你可能感兴趣的:(最近对问题求解代码)