Codeforces 22A A. Second Order Statistics(sort)

Rating 800 总目录
题目链接入口
题目描述
给定一个数列,要求将数列中从小到大,严格排第二的数找出来。严格排第二指的是:比最小的数大的最小的数。
输入
第一行输入一个正整数 n n n ( 1   ≤   n   ≤   100 ) (1 ≤ n ≤ 100) (1n100),第二行输入数列
输出
如果有这个数存在则输出这个数,如果这个数不存在输出NO
案例
输入案例

4
1 2 2 -4

输出案例

1

输入案例

5
1 2 3 1 1

输出案例

2

题解:
先将数组用sort排序,从小到大遍历,出现第一个比最小的数大则输出,结束循环即可

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>

#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define int ll
#define INF 0x3f3f3f3f
using namespace std;
int read() { int w = 1, s = 0; char ch = getchar(); while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';    ch = getchar(); }return s * w; }
//------------------------ 以上是我常用模板与刷题几乎无关 ------------------------//
int a[110];
signed main()
{
    int n = read();
    for (int i = 0; i < n; i++)
        scanf_s("%lld", &a[i]);
    sort(a, a + n);
    bool flag = false;
    for (int i = 1; i < n; i++)
    {
        if (a[i] > a[0])
        {
            printf("%d\n", a[i]);
            flag = true;
            break;
        }
    }
    if (flag == false)
        printf("NO\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值