2018-计算机系机试(第二批)-D-最小差值

时间:2022-10-27 23:04:07

单点时限: 2.0 sec

内存限制: 256 MB

输入 n 个整数,输出最小差值。最小差值指所有数之间差的绝对值的最小数。

例如:3 个整数 126 的最小差值是 1

输入格式

第一个数是 n (2≤n≤20 ),后面是 n 个整数(值范围为 −109 ~ 109 )。n+1 个整数之间都有一个空格。

输出格式

输出最小差值。

样例

Input
3 1 2 6
Output
1
Input
4 1 1 1 1
Output
0
Input
4 -1 5 10 3
Output
2
 #include<stdio.h>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
int a[];
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&a[i]);
int m=abs(a[]-a[]);
int t;
for(int i=;i<n-;i++)
{
for(int j=i+;j<n;j++)
{
t=abs(a[i]-a[j]);
if(t<m)m=t;
}
}
printf("%d\n",m);
return ; }