【CCF】——Minimum difference

Problem description
  Given n numbers, please find the two numbers with the smallest difference (the absolute value of the 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 between adjacent integers.
Output format
  outputs an integer to indicate the answer.
Sample input

5
1 5 4 8 20

Sample output

1

Example description
  The two numbers with the smallest difference between    are 5 and 4, and the difference between them is 1.
Sample input

5
9 3 6 1 3

Sample output

0

Example explanation
  has two identical numbers 3, the difference between them is 0.
Data size and convention
  For all evaluation cases, 2 ≤ n ≤ 1000, and each given integer is a positive integer not exceeding 10,000.

Because the given integers are all positive integers not exceeding 10000, the maximum difference is 9999
#include <iostream>
#include <cmath>
using namespace std;

int main(){
    
    
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	int a[n];
	for(int i = 0;i<n;i++){
    
    
		cin >> a[i];
	} 
	int minn = 9999;
	for(int i = 0;i<n;i++){
    
    
		for(int j = i+1;j<n;j++){
    
    
			int x = abs(a[i]-a[j]);
			if(x<minn)
				minn = x;
		}
	}
	cout << minn;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45845039/article/details/108556119