zcmu 1049

1049: 最爱

Description

sls有个喜好,最爱吃草鱼。但天天就知道吃可不行啊,下面问题就来了。话说,能解出这题,sls就能吃上美味的鲜鱼。于是sls希望你能写个程序帮助他。
给你n(10<=n<=100)条草鱼的重量,每条草鱼的重量为a(1<=a<=20,a为整数)。请你找出从重到轻排后,第五重的草鱼有多少条?(若两条草鱼的重量相同,则他们排相同的序号)。

Input

多组测试数据,

详见Sample Input

Output

详见Sample Output

Sample Input

10
9 4 9 6 7 1 2 4 4 8

Sample Output

3
思路:将输入的a数组从大到下排序,再去重,未排序的数组在输入时存入b数组中,从排序去重过的a数组中找到第五位数与b数组中的数比较,相同时计数器自加,计数器的值即为答案。
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define maxn 100 + 10
#define INF 10000000000
#define FORA(i,x,y) for(int i = x; i < y; i++)
#define FORB(i,x,y) for(int i = x;i <= y;i++)
#define FORD(j,y,x) for(int j = y; j >= x; j--)
#define mset(x,v) memset(x,v,sizeof(x))
const double pi = acos(-1.0);
int a[maxn];
int b[maxn];
typedef vector<int> vi;

int cmp(int x,int y){
    return x>y;
}

int main(){
    int n;
    while(~scanf("%d",&n)){
        FORA(i,0,n){
            scanf("%d",&a[i]);
            b[i] = a[i];
        }
        sort(a,a+n,cmp);
        unique(a,a+n) - a;
        int c = 0;
        FORA(i,0,n){
            if(b[i] == a[4]) c++;
        }
        printf("%d\n",c);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/banyouxia/p/9420645.html