求最大、次大和第3大的值

作者: Goshowwww | 来源:发表于2019-03-05 19:54 被阅读0次

https://pintia.cn/problem-sets/1102099479966621696/problems/1102099508777295878

本题目要求读入n个整数,要求用最少的比较次数,输出它们的最大值、第2大的值和第3大的值。例如,对于13 13 1 10 34 10这6个数,最大值为34,第2大的值为13,第3大的值为10。

输入格式:

输入有两行。第一行为整数个数n(≤1 000 000),第二行给出n个以空格分隔的整数。

输出格式:

对每一组输入,在一行中输出最大值、第2大的值和第3大的值值,中间以一个空格分隔,但行尾没有多余空格。 如果输入数据不足三个,则输出“Invalid Input”。 如果没有第3大的值,则输出“There is no third largest element”。 如果没有第2大和第3大的值,则输出“There is no second largest and third largest element”。

输入样例:

6

13 13 1 10 34 10

输出样例:

34 13 10

提交了几次才过 这是一种方法 只用数组 下面还有个用set 的 原理都一样

#include <stdio.h>

#include <iostream>

#include <algorithm>

#include <map>

#include <set>

using namespace std;

int arr[3];

const int inf = -200000000;

int main()

    int N;

    scanf("%d", &N);

    arr[0] = inf;

    arr[1] = inf;

    arr[2] = inf;

    for (int i = 0; i < N; i++) {

    int a;

    scanf("%d", &a);

    int pos = 0, cnt = 0;

    int temp = -inf;

    for (int j = 0; j < 3; j++) {//找到最小的

    if (arr[j] < temp) {

    temp = arr[j];

    pos = j;

    }

    }

    for (int j = 0; j < 3; j++) {

    if (a == arr[j]) cnt++;

    }

    if (arr[pos] < a && cnt == 0) {

    arr[pos] = a;

    }

    }

if (N < 3) {

    printf("Invalid Input\n");

    return 0;

    }

    int cnt = 0;

    for (int j = 0; j < 3; j++) {

    if (arr[j] == inf) cnt++;

    }

    if (cnt == 2) {

    printf("There is no second largest and third largest element\n");

    return 0;

    }

    if (cnt == 1) {

    printf("There is no third largest element\n");

    return 0;

    }

    sort(arr, arr + 3);

  for (int j = 0; j < 3; j++) {

    printf("%d%c", arr[2 - j], j == 2 ? '\n':' ');

    }

    return 0;

}

用set  写起来快乐一些

#include <stdio.h>

#include <iostream>

#include <algorithm>

#include <set>

using namespace std;

set <int> st;

int main() {

int N;

    scanf("%d", &N);

    for (int i = 0, a; i < N; i++) {

    scanf("%d", &a);

    if (st.find(a) != st.end()) continue;

    if (st.size() < 3)  {

    st.insert(a);

    continue;

    }

    set <int>::iterator it = st.begin();

    if (*it < a) {

    st.insert(a);

    st.erase(*it);

    }

    }

    if (N < 3) {

    printf("Invalid Input\n");

    return 0;

    }

    int cnt = st.size();

    if (cnt == 1) {

    printf("There is no second largest and third largest element\n");

    }

    else if (cnt == 2) {

    printf("There is no third largest element\n");

    }

    else {

    set <int>::iterator it = st.begin();

    int a, b, c;

a = *it; it++;

b = *it; it++;

c = *it; it++;

    printf("%d %d %d\n", c, b, a);

    }

return 0;

}

相关文章

  • 求最大、次大和第3大的值

    https://pintia.cn/problem-sets/1102099479966621696/proble...

  • 2019-05-14

    日志文本筛选-sort awk 求最大值: 求最小值: 求和: 求平均值: 求最大值 求最大值 求最小值 中位数

  • Leetcode.124.Binary Tree Maximum

    题目 给定义一个二叉树, 求二叉树的子路径的最大和. 思路 递归. 分别对左右子树递归. 总结 递归求最大值, 需...

  • 线性表最值问题

    找最小值 找最大值 顺序表求最大值 顺序表求最小值 带头结点单链表求最大值 带头结点单链表求最小值 q是 最大值/...

  • 4.3 动态规划 DP(2)

    套路 关键字:第N个,最大和 后面的值由前面的值依次推导而来,为了充分利用上一次的结果,于是出现了最优解:动态规划...

  • python:numpy数组常用的统计函数

    数据准备: 求和 求均值 求中值 求最大值和最小值 求极值(最大值和最小值之差)、 6、标准差

  • 2018-03-24

    求最大值 假设表名为user,字段为pv,最大值为pv_max,求最大值db.user.aggregate([{"...

  • JVM常用参数

    GC常用参数 -Xmn 年轻代大小 -Xms 堆的最小值 -Xmx 堆的最大值通常最大和最小设置相同值 减少jvm...

  • LeetCode--最大子序和(python版)

    官方解答:代码非常简洁,遍历时当和大于当前最大值,就替换当前最大值,否则sum归零,保持当前值,同时继续寻找最大和...

  • 求连续子序列最大和

    给定一个无序数组,求最大的连续子数组的和 解法一: 思路:暴力解法,最大序列肯定以数组中某个数为起点,则依次遍历以...

网友评论

    本文标题:求最大、次大和第3大的值

    本文链接:https://www.haomeiwen.com/subject/jgxauqtx.html