Minimum difference

Problem Description
  Given n numbers, find the two numbers with the smallest difference (absolute value of difference), and output the absolute value of their difference.
input format
  The first line of input contains an integer n .
  The second line contains n positive integers separated by a space.
output format
  Output an integer representing the answer.
sample input
5
1 5 4 8 20
Sample output
1
Sample description
  The two numbers with the smallest difference are 5 and 4, and the difference between them is 1.
sample input
5
9 3 6 1 3
Sample output
0
Sample description
  There are two identical numbers 3 and the difference between them is 0.
Data size and conventions

  For all evaluation cases, 2 ≤ n ≤ 1000, each given integer is a positive integer up to 10000.


water

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

const int maxn=1000+5;

int a[maxn];

intmain()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	   scanf("%d",&a[i]);
	sort(a,a+n);
	int temp=10000+5;
	for(int i=0;i<n-1;i++)
	   temp=min(temp,a[i+1]-a[i]);
	cout<<temp<<endl;
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324728444&siteId=291194637