算法思想
同樣找到第k大的數類似的解法
代碼
/***************************************
輸入: ? n:數組元素的個數 ? k:第幾大的數
a:待查找的數組元素
****************************************/
#include ? <stdio
#include ? <stdlib
#include ? <time
#define ? N ?
void ? Rand_select( ? int*
int ? partition( ? int*
int ? swap( ? int&
int ? k
int ? main()
{
int ? n
while( ? scanf( ?
{
srand(time(NULL));
k
for( ? i ? = ?
scanf( ?
Rand_select( ? a
printf( ?
}
return ?
}
void ? Rand_select( ? int ? a[]
{
int ? m;
if ? (p ? <= ? q)
{
m ? = ? partition( ? a
if( ? k ? == ? m ? )
{ ? ? ? ? ? ? ans ? = ? a[m]; ? ? ? ? ? return; ? ? ? ? ? ? ? }
else ? if( ? k ? > ? m ? )
Rand_select( ? a
else
Rand_select( ? a
}
}
int ? partition( ? int ? a[]
{
int ? last
if( ? q ? != ? p ? )
swap( ? a[rand()%(q
for( ? i ? = ? p+
if( ? a[i] ? >= ? a[p] ? )
swap( ? a[i]
swap( ? a[last]
return ? last;
}
int ? swap( ? int ? &p
{
int ? temp ? = ? p;
p ? = ? q;
q ? = ? temp;
return ?
}
#include <iostream>;
using namespace std;
void swap(int& a
{
int temp = a;
a = b;
b = temp;
}
int k_smallest(int k
{
int pivot = a[right]; //the last item as pivot
int i = left;
int j = right
for(;;)
{
for(; a[i]<pivot; i++);
for(; a[j]>;=pivot; j
if(i<j)
swap(a[i]
else
break;
}
swap(a[i]
//now i is the pivot index in the array
//i
if(k==i
return a[i];
else if(k < i
return k_smallest(k
else? ?//target after pivot
return k_smallest((k
}
//find the k_th smallest item in the array
int Kth_smallest(int k
{
return k_smallest(k
}
int main()
{
int a[
for(int i=
cout<<Kth_smallest(i
}
【轉自】foxhaw/blog/cns!
int MaxK(int* data
{
int *source = data;
while(true)
{
int l = source[
int left =
int right = len
while(left != right)
{
while((l > source[left]) && (right > left))left++;
while((l < source[right]) && (right > left))right
swap(source
}
if(left == k
return max(source[
else if(left > k
{
if(source[
swap(source
//return MaxK(source
len = left;
}
else
{
//return MaxK(source+left
source += left;
len = len
k = k
}
}
return
}
int heapsort(int *data
{
int data[] = {
//Part
int i
int tmp;
for(k = (n>>
{
tmp = data[k];
for(j = k; (j<<
{
j
if(j
i = j
else
{
if(data[j
i = j
else
i = j
}
? if(tmp < data[i])
data[j] = data[i];
else
break;
}
data[j] = tmp;
}
//Part
int range = n
for(k = n
{
tmp = data[k];
data[k] = data[
for(j =
{
j
if(j
i = j
else
{
if(data[j
i = j
else
i = j
}
if(tmp < data[i])
data[j] = data[i];
else break;
}
data[j] = tmp;
}
return data[range];
}
int nthstl(int* source
{
typedef vector<int> IntVector ;
? //Define an iterator for template class vector of strings
typedef IntVector::iterator IntVectorIt ;
? IntVector Numbers(len) ;
for(int i =
{
Numbers[i] = source[i];
}
IntVectorIt start
? start = Numbers
end = Numbers
? nth_element(start
return Numbers[k
}
int stlk(int* source
{
typedef vector<int> IntVector ;
? //Define an iterator for template class vector of strings
typedef IntVector::iterator IntVectorIt ;
? IntVector Numbers(len) ;
for(int i =
{
Numbers[i] = source[i];
}
IntVectorIt start
? start = Numbers
end = Numbers
? partial_sort(start
return Numbers[k
}
可以先用快速排序進行排序
代碼如下
//快速排序
#include <iostream>
using namespace std;
int Partition ? (int *L
{
int temp ? = ? L[low];
int pt ? = ? L[low];
while ? (low ? < ? high)
{
while ? (low ? < ? high ? && ? L[high] ? >= ? pt)
L[low] ? = ? L[high];
while ? (low ? < ? high ? && ? L[low] ? <= ? pt)
++low;
L[low] ? = ? temp;
}
L[low] ? = ? temp;
return low;
}
void QSort ? (int *L
{
if ? (low ? < ? high)
{
int pl ? = ? Partition ? (L
QSort ? (L
QSort ? (L
}
}
int main ? ()
{
int narry[
int sum ? = ?
cout ? << ?
cin ? >> ? t;
while ? (t ? != ?
{
narry[sum] ? = ? t;
addr[sum ?
sum++;
cin ? >> ? t;
}
sum ?
QSort ? (narry
for ? (int ? i ? = ?
cout ? << ? narry[i] ? << ?
cout ? << ? endl;
int k;
cout ? << ?
cin ? >> ? k;
int aa ? = ?
int kk ? = ?
for ? (;;)
{
if ? (aa ? == ? k)
break;
if ? (narry[kk] ? != ? narry[kk ? + ?
{
aa ? += ?
kk++;
}
}
cout ? << ?
cout ? << ?
for ? (i ? = ?
{
if ? (addr[i] ? == ? narry[sum ?
cout ? << ? i ? << ?
}
return
}
這道題去年baidu筆試也出現過
#include ? <math
#include ? <time
#include ? <string>
#include ? <iostream>
#include ? <vector>
using ? namespace ? std;
#define ? n ?
#define ? k ?
int ? main(int ? argc
{
srand((unsigned ? )time(NULL));
if ? ( ? k ? > ? n ? )
{
cout ? << ?
return ?
}
vector<int> ? src;
cout ? << ?
for ? ( ? int ? i ? = ?
{
src
cout ? << ? src[i] ? << ?
}
vector<int> ? maxNum; //順序存入最大k個數
for ? ( ? i ? = ?
{
maxNum
}
for ? ( ? i ? = ?
{
for ? ( ? int ? j ? = ?
{
if ? ( ? src[i] ? >= ? maxNum[j] ? ) //比當前的大
{
for ? ( ? int ? i
{
maxNum[i
}
maxNum[j] ? = ? src[i];
break;
}
}
}
cout ? << ? endl ? << ?
return ?
}
分析
From:http://tw.wingwit.com/Article/program/sjjg/201405/30745.html