1049: 最爱
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 561 Solved: 248
[ Submit][ Status][ Web Board]
Description
sls
有个喜好,最爱吃草鱼。但天天就知道吃可不行啊,下面问题就来了。话说,能解出这题,sls就能吃上美味的鲜鱼。于是sls希望你能写个程序帮助他。
给你n(10<=n<=100)条草鱼的重量,每条草鱼的重量为a(1<=a<=20,a为整数)。请你找出从重到轻排后,第五重的草鱼有多少条?(若两条草鱼的重量相同,则他们排相同的序号)。
给你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
HINT
Source
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;
int a[105];
bool compare(int b,int c)
{
return b>c;
}
int main()
{
int n,temp,counts1,counts2;
while(scanf("%d",&n)!=EOF)
{
counts1=0;
counts2=0;
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n,compare);
for(int j=0;j<n;j++)
{
if(a[j]!=a[j+1])
counts1++;
if(counts1==5)
{
temp=a[j];
break;
}
}
for(int k=0;k<n;k++)
{
if(a[k]==temp)
counts2++;
if(a[k]<temp)
break;
}
printf("%d\n",counts2);
}
return 0;
}
解题心得: