Find numbers

HRBUST - 2365

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;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值