Run ID:129153
提交时间:2025-08-22 14:17:02
#include <iostream> #include <vector> #include <algorithm> #include <climits> // 用于INT_MAX using namespace std; int main() { int n; cin >> n; // 读取数组长度 vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; // 读取数组元素 } sort(a.begin(), a.end()); // 对数组进行排序 int min_distance = INT_MAX; // 初始化最小距离为最大整数 for (int i = 0; i < n - 1; ++i) { int distance = a[i + 1] - a[i]; // 计算相邻元素的差值 if (distance < min_distance) { min_distance = distance; // 更新最小距离 } } cout << min_distance << endl; // 输出结果 return 0; }