- #include "stdafx.h"
- #include "iostream"
- #include "windows.h"
- using namespace std;
- #define N 10
- void main()
- {
- int num[N];
- int low,hight,plow,phight,kelement=0,k,temp;
- cout<<"Input the "<<N<<" numbers:";
- for (int i=0;i<N;i++)
- {cin>>num[i];}
- cout<<"Input the K:";
- cin>>k;
- plow=0;
- phight=N-1;
- while (kelement!=N-k)
- {
- low=plow;
- hight=phight;
- while (low!=hight)
- {
- if (num[low]>num[hight])
- {
- temp=num[low];
- num[low]=num[hight];
- num[hight]=temp;
- low++;
- }
- else
- {hight--;}
- }
- kelement=low;
- if (low<N-k)
- {plow=low+1;}
- else
- {phight=low-1;}
- }
- cout<<"The k max element is: "<<num[N-k]<<endl;
- Sleep(10000);
- }
- //该片段来自于http://www.codesnippet.cn/detail/130520149559.html
来源: http://www.codesnippet.cn/detail/130520149559.html