Codeforces 22A A. Second Order Statistics(sort)

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

猜你喜欢

转载自blog.csdn.net/m0_46272108/article/details/108191974
今日推荐