输入n,m 表示输入n个数输出前m个最大的数
Input
The input file contains many test cases. Each case has 2 lines. The first line consists of 2 integer number n and m ( 0 < n, m < 1000000). The next line has n numbers, and all numbers between -500000 and -500000.
Output
For each case, you should output the m integer numbers, order by descent.
Sample Input
5 3 3 -35 92 213 -644
Sample Output
213 92 3
其实int没有那么小 竟然可以用这种办法来不用排序就可以解答。所以,不要题目用sort就只想着排序,只想着用sort函数。 说不定就可以像这样发现很直接:简单”的方法。
View Code
1 #include <stdio.h> 2 #include <string.h> 3 int a[1000001]; 4 int i ,j ,n ,m, flag=0,count ,num; 5 int main() 6 { 7 while( scanf("%d%d",&n,&m)!=EOF) 8 { 9 count =0; 10 flag=0; 11 memset(a,0,sizeof(a)); 12 for(i=0;i<n;i++) 13 { 14 scanf("%d",&num); 15 a[num+500000]++; 16 } 17 for(i=1000000; ;i--) 18 { 19 for(j=a[i];j > 0;j--) 20 { 21 printf("%d ",i-500000); 22 count++; 23 if(count==m) 24 { 25 flag=1; 26 break; 27 } 28 } 29 if(flag) 30 { 31 printf(" "); 32 break; 33 } 34 } 35 } 36 } 37 38