#include <iostream>
#include <algorithm>
#include <limits>
using namespace std;
const int MAXSIZE = 1000;
int main(int argc, char const *argv[])
{
double arr[MAXSIZE];
while(true)
{
int size;
printf("请输入给定double型数组的维度(其中最大为%d): ", MAXSIZE);
scanf("%d", &size);
printf("请输入数组的数据: \n");
for (int i = 0; i < size; ++i)
{
scanf("%lf", &arr[i]);
}
sort(arr, arr+size);
double min = (numeric_limits<double>::max)(); //double的最大值
double re1, re2;
for (int i = 1; i < size; ++i)
{
if(min > arr[i] - arr[i-1])
{
min = arr[i] - arr[i-1];
re1 = arr[i-1];
re2 = arr[i];
}
}
printf("差值最小的两个数分别为: %.15f, %.15f\n", re1, re2);
printf("差值为: %.15f\n\n\n", re2 - re1);
}
return 0;
}
|