Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1164 Accepted Submission(s): 597
3 1 2 5 -1 5 3 1 2 3 4 5 0 0
5 5 4 3
解题代码
#include<stdio.h>
#include<stdlib.h>
int comp(const void *a,const void *b) {
return *(int *)b-*(int *)a;
}
int main() {
int n,m,i;
int a[100000];
while(scanf("%d%d",&n,&m),n+m) {
for(i=0;i<n;i++)
scanf("%d",&a[i]);
qsort(a,n,sizeof(int),comp);
if(m<n) {
for(i=0;i<m;i++)
printf(i==0?"%d":" %d",a[i]);
}
else {
for(i=0;i<n;i++)
printf(i==0?"%d":" %d",a[i]);
}
printf("\n");
}
return 0;
}
先排序,再判断