[backend]将testdata/下的测例替换为了赛方测试用例,更新了测试脚本
This commit is contained in:
58
testdata/h_functional/16_k_smallest.sy
vendored
Normal file
58
testdata/h_functional/16_k_smallest.sy
vendored
Normal file
@ -0,0 +1,58 @@
|
||||
const int maxN = 1000;
|
||||
const int space = 32;
|
||||
int array[maxN];
|
||||
|
||||
void swap(int i, int j) {
|
||||
int tmp = array[i];
|
||||
array[i] = array[j];
|
||||
array[j] = tmp;
|
||||
}
|
||||
|
||||
int findPivot(int start, int end) {
|
||||
int pivot = array[end];
|
||||
|
||||
int pIndex = start, i = start;
|
||||
while (i < end) {
|
||||
if (array[i] <= pivot) {
|
||||
swap(i, pIndex);
|
||||
pIndex = pIndex + 1;
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
swap(pIndex, end);
|
||||
return pIndex;
|
||||
}
|
||||
|
||||
void findSmallest(int low, int high, int k, int n) {
|
||||
if (low == high)
|
||||
return;
|
||||
else {
|
||||
int pIndex = findPivot(low, high);
|
||||
if (k == pIndex) {
|
||||
int i = 0;
|
||||
while (i < pIndex) {
|
||||
putint(array[i]);
|
||||
putch(space);
|
||||
i = i + 1;
|
||||
}
|
||||
}
|
||||
else if (k < pIndex) {
|
||||
findSmallest(low, pIndex - 1, k, n);
|
||||
}
|
||||
else {
|
||||
findSmallest(pIndex + 1, high, k, n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n = getint(), k = getint();
|
||||
int i = 0;
|
||||
while (i < n) {
|
||||
array[i] = getint();
|
||||
i = i + 1;
|
||||
}
|
||||
int low = 0, high = n - 1;
|
||||
findSmallest(low, high, k, n);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user