用了三种方法实现的... 1.stl nth_element() 16MS #include <cstdlib> #include <iostream> #include <algorithm> #include <cstdio> #include <vector> using namespace std; int main(int argc, char *argv[]) { int count; scanf("%d", &count); vector<int> vec; int each; for(int i=0; i<count; i++) { scanf("%d", &each); vec.push_back(each); } int mid = count / 2; nth_element(vec.begin(), vec.begin() + mid, vec.end()); cout << vec[mid] << endl; return EXIT_SUCCESS; } 2.stl sort() 16MS #include <cstdlib> #include <iostream> #include <algorithm> #include <cstdio> #include <vector> using namespace std; int main(int argc, char *argv[]) { int count; scanf("%d", &count); vector<int> vec; int each; for(int i=0; i<count; i++) { scanf("%d", &each); vec.push_back(each); } int mid = count / 2; sort(vec.begin(), vec.end()); cout << vec[mid] << endl; return EXIT_SUCCESS; } 3. heap sort 0MS #include <iostream> #include <cstdlib> #include <cstdio> using namespace std; const int MAX = 10000; void filerDown(int heap[], int start, int end) { int i = start; int j = 2 * i + 1; int temp = heap[i]; while(j <= end) { if(j < end && heap[j] < heap[j + 1]) j++; if(temp >= heap[j]) break; else { heap[i] = heap[j]; i = j; j = 2 * j + 1; } } heap[i] = temp; } void createHeap(int heap[], int count) { int curSize = count; int curPos = (curSize - 2) / 2; while(curPos >= 0) { filerDown(heap, curPos, curSize - 1); curPos--; } } void heapSort(int heap[], int count) { int mid = count / 2; for(int i=count - 1; i>=mid; i--) { swap(heap[0], heap[i]); filerDown(heap, 0, i - 1); } } int main(int argc, char *argv[]) { int count; scanf("%d", &count); int heap[MAX]; for(int i=0; i<count; i++) scanf("%d", &heap[i]); createHeap(heap, count); heapSort(heap, count); printf("%d/n", heap[count / 2]); return EXIT_SUCCESS; } heap sort时只要对前n / 2个元素排序即可...