Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
就是第K小的数。。。。。(附代码)代码较乱 将就着看吧…… #include<stdio.h> #include<string.h> void swap(int *a,int *b){ // *a+=*b; // *b=*a-*b; // *a-=*b; // 这种方法WA了 我怀疑是溢出…… int c=*a; *a=*b; *b=c; } int n; int a[10005]; void my_sort(int x,int y,int k){ int key=a[x]; int front=x; int rear=y-1; if(x>=y-1) return; while(front<=rear){ while(front<=rear&&a[front]<=key) ++front; while(front<=rear&&a[rear]>=key) --rear; if(front<rear){ swap(&a[front],&a[rear]); //a[front]+=a[rear];a[rear]=a[front]-a[rear];a[front]-=a[rear]; // 没有调用 swap 函数 也WA 真的可能是溢出了…… ++front; --rear; } } swap(&a[x],&a[rear]); //a[x]+=a[rear];a[rear]=a[x]-a[rear];a[x]-=a[rear]; if(x<=k&&k<rear) my_sort(x,rear,k); if(rear+1<=k&&k<y) my_sort(rear+1,y,k); } main(){ while(~scanf("%d",&n)){ int i,k; for(i=0;i<n;++i){ scanf("%d",&a[i]); } k=n>>1; my_sort(0,n,k); printf("%d\n",a[k]); } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator