CF22A Second Order Statistics

Second Order Statistics (CF22A) 题解

这是一道CF的题目(Code Forces 22A)

题目(英文):

Once Bob needed to find the second order statistics of a sequence of integer numbers. Lets choose each number from the sequence exactly once and sort them. The value on the second position is the second order statistics of the given sequence. In other words it is the smallest element strictly greater than the minimum. Help Bob solve this problem.

输入格式:

The first input line contains integer nn ( 1<=n<=1001<=n<=100 ) — amount of numbers in the sequence. The second line contains nn space-separated integer numbers — elements of the sequence. These numbers don't exceed 100 in absolute value.

输出格式:

If the given sequence has the second order statistics, output this order statistics, otherwise output NO.

题意翻译(中文)
给定一个数组,输出其中第二小的整数(相等的整数只计算一次)
输入:
第一行,一个整数n(1<=n<=100),表示数组长度
第二行,n个绝对值小于100的整数。
输出:
一行。如果该数组存在第二小整数,则输出。如果不存在,则输出NO.

题目不难理解

主要的是排序。(Sort快排一遍加个特判不就好了吗?)

过滤第一小的数,用剩下的数排一次。

< 如果Sort不会,下有参考文献 >

 放代码:

 

#include<bits/stdc++.h>
using namespace std;
int n,a[107],l;
int main(){scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]);sort(a+1,a+n+1);l=a[1];for(int i=1;i<=n;i++){if(a[i]!=l){printf("%d\n",a[i]);return 0;}}printf("NO\n");return 0;
}

  

参考文献:

  Sort百度百科

排序算法总结

 

OVER!


写于2019年1月2日
作者:WSJ

转载于:https://www.cnblogs.com/wangshengjun/p/cf22A.html

本文链接:https://my.lmcjl.com/post/5926.html

展开阅读全文

4 评论

留下您的评论.