博客
关于我
CCFCSP 201709-1 打酱油
阅读量:748 次
发布时间:2019-03-22

本文共 587 字,大约阅读时间需要 1 分钟。

思路:对数组进行排序后,遍历检查相邻元素的最小差值,即可得到结果。

#include 
using namespace std;typedef long long ll;const int N = 1e6 + 5;const ll mod = 1e9 + 7;int main() { int n; cin >> n; vector
a; for(int i = 1; i <= n; ++i) { ll x; cin >> x; a.push_back(x); } sort(a.begin(), a.end()); ll mi = LLONG_MAX; for(int i = 1; i < a.size(); ++i) { if(a[i] - a[i-1] < mi) { mi = a[i] - a[i-1]; } } // ...}

代码逻辑解析:

  • 首先读取输入数据,储存在数组$a中。
  • 对数组进行排序。
  • 初始化minimal difference为一个极大值。
  • 遍历排序后的数组,逐个计算相邻元素之差。
  • 如果当前差值小于已知的最小值,则更新最小值。
  • 最终输出结果。
  • 转载地址:http://huhwk.baihongyu.com/

    你可能感兴趣的文章
    Python:将列表与范围列表合并
    查看>>
    Python:将列表列表初始化为一定大小,
    查看>>
    Python:带有生成器的给定集合的幂集
    查看>>
    Python:按字符的位置拆分字符串
    查看>>
    Python:替换嵌套字典中的值
    查看>>
    Python:爬取数据出现response.status_code为403解决方法
    查看>>
    Python:爬取数据出现response.status_code为418解决方法
    查看>>
    Python:生成“元组集“;来自“元组列表“;不考虑顺序
    查看>>
    Python:生成具有趋势的随机时间序列数据(例如周期性、指数衰减等)
    查看>>
    Python:用渐进式数字重命名列表中的重复项而不对列表进行排序
    查看>>
    Python:矩形参数无效
    查看>>
    Python:语法进阶
    查看>>
    Python:调用复制函数时调用 Python 对象时超出了最大递归深度
    查看>>
    Python:这两个 import 语句有什么区别?
    查看>>
    Python:通过串口从 Velleman k8090 读取数据
    查看>>
    python:遍历具有列表值的字典
    查看>>
    Python:降低精度 pandas 时间戳数据帧
    查看>>
    python:非阻塞子进程,检查标准输出
    查看>>
    PythonABC多重继承
    查看>>
    Pythonargparse中的多个参数
    查看>>