Description
There are N Numbers here, you need find that pair of numbers they have the smallest absolute value. For example, 5 numbers -1, 2,-5,1,2. The pair of numbers meets the requirement is (2,2) because their absolute value is 0.
There are multiple cases, for each case, you just need output the smallest absolute value in one line.
Input
There are multiple cases, for each case, the first line is N(2<=N<=10000),the second line is N numbers and each number belongs to [-1000000,1000000].
Process to the end of file.
Output
For each case, output the answer with one line.
Sample Input
5
-1 2 -5 1 2
3
-1 -3 0
Sample Output
0
1
HINT
We all Participated in CSP.
解析
首先输入数组的个数,利用C++中的algorithm对数组进行排列,进行相邻的两位数相减,输出最小差值。
#include<iostream>
#include<algorithm>
int a[100050];
using namespace std;
int main()
{
int m,i;
while(cin>>m)
{
for(i=0; i<m; i++)
cin>>a[i];
int len=sizeof(a)/sizeof(int);
sort(a,a+len);
int mini=a[1]-a[0];
for(i=2; i<m; i++)
if((a[i]-a[i-1])<mini)
mini=a[i]-a[i-1];
cout<<mini<<endl;
}
}