CSP考试 2017年12月第1题 最小差值 C++实现

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
	int NUM;
	cin>>NUM;
	vector<int> a;
	for(int i=0;i<NUM;i++)
	{
		int t;
		cin>>t;
		a.push_back(t);
	}
	sort(a.begin(),a.end());
	int min=10000;
	for(int i=0;i<NUM-1;i++)
	{
		int t;
		if(a[i]>a[i+1])
		{
			t=a[i]-a[i+1];
		}else
		{
			t=a[i+1]-a[i];
		}
		if(t<min)
		{
			min=t;
		}
	}
	cout<<min;
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/woniupengpeng/article/details/79017002