
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int dist(int *x,int nx,int *y,int ny) //nx: 数组x的长度,ny:数组y的长度
{
int dist = abs(x[0]-y[0]);
int px = 0,py = 0;
while(px<nx && py<ny)
{
if(x[px]==y[py]) return 0;
if(x[px]<y[py])
{
if(dist > abs(x[px]-y[py]))
dist = abs(x[px]-y[py]);
px++;
}
if(x[px]>y[py])
{
if(dist > abs(x[px]-y[py]))
dist = abs(x[px]-y[py]);
py++;
}
}
return dist;
}
int main()
{
int x[] = {1,2,5,7,9};
int y[] = {3,11,12,18};
printf("%d\b",dist(x,5,y,4));//check
}
只做了int型的数组,如果是double型时自己将
int dist(int *x,int nx,int *y,int ny) //nx: 数组x的长度,ny:数组y的长度
{
int dist = abs(x[0]-y[0]);
改为
double dist(
double *x,int nx,
double *y,int ny) //nx: 数组x的长度,ny:数组y的长度
{
double dist = abs(x[0]-y[0]);
就可以了