博客
关于我
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去重txt文本_Python实现的txt文件去重功能示例
    查看>>
    python去掉列表的逗号,从Python列表项中删除标点符号
    查看>>
    Python卸载所有包
    查看>>
    python单线程下实现多个socket并发
    查看>>
    Python单元测试框架介绍(超详细~)
    查看>>
    Python单元测试框架
    查看>>
    python单元测试之unittest
    查看>>
    Python单元测试unittest实战
    查看>>
    Python单元测试-模拟VS补丁
    查看>>
    python协程实时输出_Python 协程以及事件循环的问题,怎么知道 IO 读取结束了呢?...
    查看>>
    Python十进制&;包提供错误的结果
    查看>>
    Python化身“安全老中医”:一键搞定工控系统漏洞,从零基础到精通,收藏这篇就够了!
    查看>>
    Python加速运行技巧
    查看>>
    python办公自动化基础遍历文件夹
    查看>>
    python办公自动化基础输出文件和文件夹
    查看>>
    python办公自动化基础搜索文件
    查看>>
    python知识:@classmethod和@staticmethod的异同
    查看>>
    python前端之css
    查看>>
    Python制作进度条,原来有这么多方法
    查看>>
    Python制作简单的学生成绩管理系统
    查看>>