CSP认证201712-1最小差值[C++题解]:遍历
生活随笔
收集整理的這篇文章主要介紹了
CSP认证201712-1最小差值[C++题解]:遍历
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 題目解答
- 題目鏈接
題目解答
來源:acwing
分析: 直接排序,然后取相鄰兩數之差的最小值。
ac代碼
#include<bits/stdc++.h> using namespace std; const int N = 1010; int n; int a[N];int main(){int res = 1e9;cin >> n;for(int i = 0; i < n; i ++) cin >> a[i];sort(a, a + n);for(int i = 0; i + 1 < n; i++){res = min(res, a[i + 1] - a[i]);}cout << res << endl; }題目鏈接
https://www.acwing.com/problem/content/3255/
總結
以上是生活随笔為你收集整理的CSP认证201712-1最小差值[C++题解]:遍历的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSP认证201709-4通信网络[C+
- 下一篇: CSP认证201712-4行车路线[C+