单点时限: 2.0 sec
内存限制: 256 MB
输入 n 个整数,输出最小差值。最小差值指所有数之间差的绝对值的最小数。
例如:3 个整数 1
,2
和 6
的最小差值是 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
1 #include<stdio.h> 2 #include<cstring> 3 #include<iostream> 4 #include<cmath> 5 using namespace std; 6 int main() 7 { 8 int n; 9 int a[25]; 10 scanf("%d",&n); 11 for(int i=0;i<n;i++) 12 scanf("%d",&a[i]); 13 int m=abs(a[0]-a[1]); 14 int t; 15 for(int i=0;i<n-1;i++) 16 { 17 for(int j=i+1;j<n;j++) 18 { 19 t=abs(a[i]-a[j]); 20 if(t<m)m=t; 21 } 22 } 23 printf("%d ",m); 24 return 0; 25 26 }