5 3 3 -35 92 213 -644
213 92 3请用VC/VC++提交HintHint
#include<iostream> #include<stdio.h> #include<algorithm> using namespace std; int a[1000010]; void swap(int *a,int *b){ int t =*a; *a=*b; *b=t; } int partition(int a[],int l,int h){ int i=l; int j=h+1; int v=a[l]; while(true){ while(a[++i]>v)if(i==h)break; while(a[--j]<v)if(h==l)break; if(i>=j)break; swap(&a[i],&a[j]); } swap(&a[l],&a[j]); return j; } void quick_sort(int a[],int l,int h){ if(h<=l)return ; int j=partition(a,l,h); quick_sort(a , l , j-1); quick_sort(a , j+1, h); } int cmp(int a,int b){ return a>b; } int main(int argc, char *argv[]) { int n,m; //freopen("1425.in", "r", stdin); while(scanf("%d %d",&n,&m)==2) { for(int i=0;i<n;++i) scanf("%d",&a[i]); //sort(a,a+n,cmp); quick_sort(a,0,n-1); if(m==1) printf("%d\n",a[0]); else{ for(int i=0;i<m-1;++i) { printf("%d ",a[i]); } printf("%d\n",a[m-1]); } } return 0; }
原文地址:http://blog.csdn.net/wdkirchhoff/article/details/41868643