程序填空题:建初始堆
建初始堆,把无序序列L.r[1..n]建成大根堆。
```c++
#include
#define MAXSIZE 1000
using namespace std;
typedef struct
{
int key;
char *otherinfo;
}ElemType;
typedef struct
{
ElemType *r;
int length;
}SqList;
void HeapAdjust(SqList &L,int s,int m)
{
ElemType rc;
int j;
rc=L.r[s];
for(j=2*s;j<=m;@@[j*=2](2))
{
if(@@[j if(@@[rc.key>=L.r[j].key](2)) break;
L.r[s]=L.r[j];
s=j;
}
L.r[s]=rc;
}
void Create_Sq(SqList &L)
{
int i,n;
cin>>n; //输入的值不大于 MAXSIZE
for(i=1;i<=n;i++)
{
cin>>L.r[i].key;
L.length++;
}
}
void show(SqList L)
{
int i;
for(i=1;i<=L.length;i++)
if(i==1)
cout< else
cout<<" "<}
//把无序序列L.r[1..n]建成大根堆
void CreatHeap(SqList &L)
{
int i,n;
n=L.length;
for(@@[i=n/2;i>0;--i](2))
HeapAdjust(L,i,n);
}
int main()
{
SqList L;
L.r=new ElemType[MAXSIZE+1];
L.length=0;
Create_Sq(L);
CreatHeap(L);
show(L);
return 0;
}
```
### 输入样例:
第一行输入一个数n(输入的值不大于 MAXSIZE)。
第二行依次输入n个数
```in
9
30 45 53 78 65 9 12 17 23
```
### 输出样例:
输出以第1个数为根的大根堆。
```out
78 65 53 45 30 9 12 17 23
```
答案:
第1空:j*=2
第2空:j
第3空:rc.key>=L.r[j].key
第4空:i=n/2;i>0;--i
```c++
#include
#define MAXSIZE 1000
using namespace std;
typedef struct
{
int key;
char *otherinfo;
}ElemType;
typedef struct
{
ElemType *r;
int length;
}SqList;
void HeapAdjust(SqList &L,int s,int m)
{
ElemType rc;
int j;
rc=L.r[s];
for(j=2*s;j<=m;@@[j*=2](2))
{
if(@@[j
L.r[s]=L.r[j];
s=j;
}
L.r[s]=rc;
}
void Create_Sq(SqList &L)
{
int i,n;
cin>>n; //输入的值不大于 MAXSIZE
for(i=1;i<=n;i++)
{
cin>>L.r[i].key;
L.length++;
}
}
void show(SqList L)
{
int i;
for(i=1;i<=L.length;i++)
if(i==1)
cout<
cout<<" "<
//把无序序列L.r[1..n]建成大根堆
void CreatHeap(SqList &L)
{
int i,n;
n=L.length;
for(@@[i=n/2;i>0;--i](2))
HeapAdjust(L,i,n);
}
int main()
{
SqList L;
L.r=new ElemType[MAXSIZE+1];
L.length=0;
Create_Sq(L);
CreatHeap(L);
show(L);
return 0;
}
```
### 输入样例:
第一行输入一个数n(输入的值不大于 MAXSIZE)。
第二行依次输入n个数
```in
9
30 45 53 78 65 9 12 17 23
```
### 输出样例:
输出以第1个数为根的大根堆。
```out
78 65 53 45 30 9 12 17 23
```
答案:
第1空:j*=2
第2空:j
第3空:rc.key>=L.r[j].key
第4空:i=n/2;i>0;--i