zcmu.oj-1049 最爱

xiaoxiao2021-02-28  35

Description

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

Input

多组测试数据,

详见Sample Input

Output

详见Sample Output

Sample Input

109 4 9 6 7 1 2 4 4 8

Sample Output

3

分析:

这道题目的本质是数组排序,可以用sort函数,由于题目说的是按质量从大到小排序(即sort的降序排序)

用到了sort函数的自定义排序:

                                                bool cmp( int a,int b )

                                                {

                                                           return a<b;

                                                }

                                                 sort( a,a+n,cmp );

代码:

#include<stdio.h>#include<algorithm>using namespace std;        bool cmp(int a,int b) //自定义排序方式        {            return a>b;        }int main(){    int a[100];    int n,i,b,num,count; //b:数组a中不重复的数;num:质量的排名;count:相同质量的数量;    while(~scanf("%d",&n))    {        for( i=0; i<n; i++)        {            scanf("%d",&a[i]);        }        sort(a,a+n,cmp);        b = a[0];        num = 1;        count = 0;        for( i=1; num<5; i++ )        {            if(b!=a[i])               {                    b=a[i];                    num++;               }        }        for( i=0; i<n; i++ )        {            if( b==a[i] )   //此时的b即为第5重的鱼的质量;            {                count++;            }        }        printf("%d\n",count);    }    return 0;}

转载请注明原文地址: https://www.6miu.com/read-1700347.html

最新回复(0)