7月5日
今天完成了資料結構第三題;尋找大富翁,下面是我的原始碼
#include <iostream>
#include <vector>
using namespace std;
const int max_size = 1000010;
int num[max_size];
void sift(int* num, int low, int high) {
int i = low;
int j = 2 * i;
int temp = num[i];
while (j <= high) {
if (j < high && num[j] < num[j + 1])
++j;
if (temp < num[j]) {
num[i] = num[j];
i = j;
j = 2 * i;
} else {
break;
}
}
num[i] = temp;
}
int main() {
int n, m, temp, count = 0;
cin >> n >> m;
for (int i = 1; i <= n; ++i)
cin >> num[i];
if (n < m) m = n;
for (int i = n / 2; i >= 1; --i)
sift(num, i, n);
for (int i = n; i >= 2; --i) {
temp = num[1];
num[1] = num[i];
num[i] = temp;
++count;
if (count == 1)
cout << num[i];
else
cout << " " << num[i];
if (count == m)
break;
sift(num, 1, i - 1);
}
if (m == n)
cout << " " << num[1];
cout << endl;
return 0;
}