【CCF】最小差值

问题描述:

问题描述

  给定n个数,请找出其中相差(差的绝对值)最小的两个数,输出它们的差值的绝对值。

输入格式

  输入第一行包含一个整数n
  第二行包含n个正整数,相邻整数之间使用一个空格分隔。

输出格式

  输出一个整数,表示答案。

样例输入

5
1 5 4 8 20

样例输出

1

样例说明

  相差最小的两个数是5和4,它们之间的差值是1。

样例输入

5
9 3 6 1 3

样例输出

0

样例说明

  有两个相同的数3,它们之间的差值是0.

数据规模和约定

  对于所有评测用例,2 ≤ n ≤ 1000,每个给定的整数都是不超过10000的正整数。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <string>
 5 #include <vector>
 6 using namespace std;
 7 const int maxn = 100005;
 8 int s[maxn];
 9 int main() {
10     freopen("/Users/kangyutong/Desktop/in.txt","r",stdin);
11     int n, m;
12     cin >> n;
13     //cout << n << endl;
14     for(int i = 0; i < n; i++) {
15         cin >> s[i];
16     }
17     sort(s, s+n);
18     m = s[1]-s[0];
19     for(int i = 2; i < n; i++){
20         int temp = s[i]-s[i-1];
21         if(temp < m) m = temp;
22     }
23     cout << m << endl;
24     return 0;
25 }

原文地址:https://www.cnblogs.com/md-zz/p/8510201.html

时间: 2024-07-31 18:48:04

【CCF】最小差值的相关文章

CCF|最小差值|Java

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n=Integer.parseInt(in.nextLine().trim()); int[] a=new int[n]; int[] b=new int[n-1]; for(int i=0;i<n;i++) { a[i]=in.nextInt

笔试题:排序后取最小差值数

笔试题:要从5个人中选取2个人作为礼仪,其中每个人的身高范围为160-190,要求2个人的身高差值最小(如果差值相同的话,选取其中最高的两人),以升序输出两个人的身高. Smple input:161 189 167 172 188 Sample outPut: 188 189 public class demo06{ public static void main(String[] args){ int[] arr={161,189,167,172,188};//原始值 //将原始值升序排序

POJ 3264 Balanced Lineup(最大最小差值 线段树水题)

Language: Default Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 37122   Accepted: 17383 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order.

luogu4234 最小差值生成树

题目大意 在一个带权无向图中,它的最小差值生成树为最大边与最小边差值最小的生成树.求一个图的最小差值生成树. 题解 30分解法 引理1 最小生成树的最大边的边权是所有生成树中最大边边权中的最小值. 证明:任意一棵生成树都可以在最小生成树的基础上,通过不断取一个树外边e,将其替换掉其与生成树所在环中的一条边的方式而得到.我们就看看第一条用来替换的边的情况吧.在不在最小生成树中的边中任取一个边权小于最小生成树最大边m的边e,则e必然与最小生成树的树边形成环.若m不在环中,那么就是替换掉任意一条边,答

LeetCode-908. 最小差值 I

给定一个整数数组 A,对于每个整数 A[i],我们可以选择任意 x 满足 -K <= x <= K,并将 x 加到 A[i] 中. 在此过程之后,我们得到一些数组 B. 返回 B 的最大值和 B 的最小值之间可能存在的最小差值. 示例 1: 输入:A = [1], K = 0 输出:0 解释:B = [1] 示例 2: 输入:A = [0,10], K = 2 输出:6 解释:B = [2,8] 示例 3: 输入:A = [1,3,6], K = 3 输出:0 解释:B = [3,3,3] 或

洛谷.4234.最小差值生成树(LCT)

题目链接 先将边排序,这样就可以按从小到大的顺序维护生成树,枚举到一条未连通的边就连上,已连通则(用当前更大的)替换掉路径上最小的边,这样一定不会更差. 每次构成树时更新答案.答案就是当前边减去生成树上最小边的权值. LCT上维护最小边的编号.求最小边把树上的边用vis[]标记即可. 不熟啊. (另外暴力可以排序后枚举一个分界点,在它之后求最小生成树,在它之前求最大生成树) #include <cstdio> #include <cctype> #include <algor

洛谷P4234 最小差值生成树【LCT】

题目描述 给定一个标号为从 1 到 n 的.有 m 条边的无向图,求边权最大值与最小值的差值最小的生成树. 输入格式: 第一行两个数 n, m ,表示图的点和边的数量. 第二行起 m 行,每行形如$ u_i, v_i, w_i$ ,代表 $u_i$ 到 $v_i$有一条长为 $w_i$的无向边. 输出格式: 输出一行一个整数,代表你的答案. 数据保证存在至少一棵生成树. 输入样例#1: 4 6 1 2 10 1 3 100 1 4 90 2 3 20 2 4 80 3 4 40 输出样例#1:

luoguP4234 最小差值生成树

https://www.luogu.org/problemnew/show/P4234 按照边的权值从小到大排序,依次加入,并删除能够删除的权值最小的一条边,用 set 维护当前所有边的边权,并查集维护联通性,LCT 维护两点间最小值和 link cut 操作即可 #include <bits/stdc++.h> #define mp make_pair using namespace std; typedef unsigned long long ull; typedef long long

201712-1最小差值

#include<vector> #include<iostream> #include<math.h> using namespace std; int main() { int n;//元素个数 cin>>n;//输入n vector <int> a;//a[] int min=10000; for(int i=0;i<n;i++)//input n ints { int x; cin>>x; for(int j=0;j&l