"科林明伦杯"哈尔滨理工大学第八届程序设计竞赛(热身).Find numbers
程序员文章站
2022-06-01 10:26:04
...
Find numbers | ||||||
| ||||||
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. |
考点:简单排序
#include<algorithm>
#include<iostream>
using namespace std;
const long long Maxn= 1e5+2;
int main()
{
int n;
long long a[Maxn];
while(cin >>n)
{
long long minn=1000005;
for(int i=0; i<n; i++)
cin>>a[i];
sort(a,a+n);
for(int i=0; i<n-1; i++)
{
if((a[i+1]-a[i])<minn)
minn=a[i+1]-a[i];
}
cout<<minn<<endl;
}
return 0;
}
上一篇: DZNEmptyDataSet空白Tableview背景的使用
下一篇: PAT A1141 心得