Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okok897/8c062157946effc86c7d26335db94efd to your computer and use it in GitHub Desktop.
Save okok897/8c062157946effc86c7d26335db94efd to your computer and use it in GitHub Desktop.
設n個整數,求整數數列區間[L,R]中第K大的數值,若不存在第K大則輸出最小值.c
#include <iostream>
using namespace std;
main() {
int n;
while(cin>>n){ //輸入n個數字
int a[n];
for(int i = 0 ; i < n ; ++i){
cin >>a[i]; //輸入數列
}
int start,end,k; //輸入範圍&設定k
cin >>start;
cin >>end;
cin >>k;
int temp[end-start];
for(int i = start ;i<=end;++i){
temp[i-start] = a[i];
}
//氣泡排序
for(int i = 0; i <= end-start ; ++i){
for(int j = 0; j < end-start-i; ++j ){
if(temp[j]<temp[j+1]){
int xxx = temp[j];
temp[j] = temp[j + 1];
temp[j + 1] = xxx;
}
}
}
if(k>end-start){
cout << temp[end-start];
}
else{
cout << temp[k-1];
}
}
}
//3 1 4 2 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment