Created
September 18, 2016 16:38
-
-
Save okok897/8c062157946effc86c7d26335db94efd to your computer and use it in GitHub Desktop.
設n個整數,求整數數列區間[L,R]中第K大的數值,若不存在第K大則輸出最小值.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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