Poj 2371--Questions and answers(QSort)

来源:互联网 发布:cms下载 编辑:程序博客网 时间:2024/06/05 22:59

题目:http://poj.org/problem?id=2371


题意:先输入N个数,再输入K个数,问这K个数Ki,问N个数中第Ki个数是什么;

思路:先排序,好久没写快排了,复习复习


源代码:(47MS)

#include<iostream>using namespace std;const int MAX_N=100005;int a[MAX_N];int n;int Partition(int low,int high){int key=a[low];while(low<high){while(low<high && a[high]>=key)high--;a[low]=a[high];while(low<high && a[low]<=key)low++;a[high]=a[low];}a[low]=key;return low;}void QSort(int low,int high){if(low<high){int pivotloc = Partition(low,high);QSort(low,pivotloc-1);QSort(pivotloc+1,high);}}int main(){int i,j,k;cin>>n;for(i=1;i<=n;i++)scanf("%d",&a[i]);char symbol[5];cin>>symbol;QSort(1,n);cin>>k;for(i=0;i<k;i++){cin>>j;cout<<a[j]<<endl;}return 0;}


原创粉丝点击