sort ()的反序需要sort(a,a+n,cmp)重写;
AC:
#include<stdio.h> #include<algorithm> using namespace std; int n,m; int a[100001]; int main() { while(scanf("%d %d",&n,&m)!=EOF) { if(n==0 && m==0) return 0; int i,j,k; for(i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); if (m>n) { for(i=0;i<n-1;i++) printf("%d ",a[i]); printf("%d ",a[n-1]); } else { for(i=0;i<m-1;i++) printf("%d ",a[n-1-i]); printf("%d ",a[n-m]); } } return 0; }