题目
将一系列给定数字顺序插入一个初始为空的小顶堆H[]。随后判断一系列相关命题是否为真。命题分下列几种:
x is the root:x是根结点;
x and y are siblings:x和y是兄弟结点;
x is the parent of y:x是y的父结点;
x is a child of y:x是y的一个子结点。
输入格式:
每组测试第1行包含2个正整数N(≤ 1000)和M(≤ 20),分别是插入元素的个数、以及需要判断的命题数。下一行给出区间[−10000,10000]内的N个要被插入一个初始为空的小顶堆的整数。之后M行,每行给出一个命题。题目保证命题中的结点键值都是存在的。
输出格式:
对输入的每个命题,如果其为真,则在一行中输出T,否则输出F。
输入样例:
5 4
46 23 26 24 10
24 is the root
26 and 23 are siblings
46 is the parent of 23
23 is a child of 10
输出样例:
F
T
F
堆排序讲解建立
堆排序讲解
建立小根堆,从后往上进行插入,好理解,简单,效率不是最好的
void update(int h[],int last)
{
int father = last/2; // 父亲节点index是孩子节点的index/2
while(father && h[father]>h[last])
{
swap(h[father],h[last]);
last = father;
father = last/2;
}
}
注意判断兄弟,父孩节点的时候,要用 数组[index] 的值来判断,不能只用下标来判断。
代码
#include <iostream>
#include <cstring>
#include <string.h>
#include <math.h>
#include <string>
using namespace std;
string s;
int ui[30010];
int n,m,tem;
char st[200];
int findIndex(int val)
{
for(int i=n;i>=1;i--)
{
if(ui[i]==val)
return i;
}
return -1;
}
// 堆排序
//void headhap(int u[],int m)
//{
//
// for(int i=(n-1)/2;i>=0;i--)
// {
//
// for(int j=i;j<n;j=j*2+1)
// {
// int il = j*2+1;
// int ir = il+1;
// if(u[j]<=u[il] && u[j]<=u[ir])
// break;
// else
// {
// if(u[j]>u[il] && il<n)
// {
// swap(u[j],u[il]);
// }
// if(u[j]>u[ir] && ir<n)
// {
// swap(u[j],u[ir]);
// }
// }
//
// }
// }
//}
void update(int h[],int last)
{
int father = last/2;
while(father && h[father]>h[last])
{
swap(h[father],h[last]);
last = father;
father = last/2;
}
}
int main()
{
memset(ui,0,sizeof(ui));
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&ui[i]);
update(ui,i);
}
// 堆排序
// for(int i=1;i<=n;i++)
// printf("%d ",ui[i]);
// 进行判断输出
int x,y;
for(int i=0;i<m;i++)
{
cin>>x>>s;
if(s[0]=='a')
{
cin>>y;
getline(cin,s);
x = findIndex(x);
y = findIndex(y);
if(ui[x/2]==ui[y/2]) cout<<"T"<<endl;
else cout<<"F"<<endl;
}
else
{
cin>>s>>s;
if(s[0]=='r') // 判断root
{
if(findIndex(x)==1) cout<<"T"<<endl;
else cout<<"F"<<endl;
}
else if(s[0]=='p')
{
cin>>s>>y;
x = findIndex(x);
y = findIndex(y);
if(ui[x]==ui[y/2]) cout<<"T"<<endl;
else cout<<"F"<<endl;
}
else
{
cin>>s>>y;
x = findIndex(x);
y = findIndex(y);
if(ui[x/2] == ui[y]) cout<<"T"<<endl;
else cout<<"F"<<endl;
}
}
}
return 0;
}
错误代码:段错误不知道为什么 21分
代码如下(示例):
#include <iostream>
#include <cstring>
#include <string.h>
#include <math.h>
#include <string>
using namespace std;
string s;
int ui[30010];
int n,m,tem;
char st[200];
int findIndex(int val)
{
for(int i=n;i>=1;i--)
{
if(ui[i]==val)
return i;
}
return -1;
}
// 堆排序 错误排序
//void headhap(int u[],int m)
//{
//
// for(int i=(n-1)/2;i>=0;i--)
// {
//
// for(int j=i;j<n;j=j*2+1)
// {
// int il = j*2+1;
// int ir = il+1;
// if(u[j]<=u[il] && u[j]<=u[ir])
// break;
// else
// {
// if(u[j]>u[il] && il<n)
// {
// swap(u[j],u[il]);
// }
// if(u[j]>u[ir] && ir<n)
// {
// swap(u[j],u[ir]);
// }
// }
//
// }
// }
//}
void update(int h[],int last)
{
int father = last/2;
while(father && h[father]>h[last])
{
swap(h[father],h[last]);
last = father;
father = last/2;
}
}
int main()
{
memset(ui,0,sizeof(ui));
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&ui[i]);
update(ui,i);
}
// 堆排序
// for(int i=1;i<=n;i++)
// printf("%d ",ui[i]);
// 进行判断输出
int x,y;
for(int i=0;i<m;i++)
{
scanf("%d",&tem);
cin.get(st,180);
if(st[8]=='r')
{
int k = findIndex(tem);
if(k==-1)
{
cout<<"F"<<endl;
continue;
}
if(k==1)
printf("T\n");
else
printf("F\n");
}
else if(st[1]=='a' && st[2]=='n')
{
int tem2=0,aindex,bindex;
int len = strlen(st);
for(int j=0;j<len;j++)
{
if(st[j]>='0' && st[j]<='9')
tem2 = tem2*10 + st[j]-'0';
}
aindex = findIndex(tem);
bindex = findIndex(tem2);
if(aindex==-1 or bindex == -1)
{
cout<<"F"<<endl;
continue;
}
if(ui[aindex/2]==ui[bindex/2]) // 错误1:不能用两节点相隔为1来判断为兄弟节点
printf("T\n");
else
printf("F\n");
}
else if(st[4]=='t')
{
int tem2=0,aindex,bindex;
int len = strlen(st);
for(int j=0;j<len;j++)
{
if(st[j]>='0' && st[j]<='9')
tem2 = tem2*10 + st[j]-'0';
}
aindex = findIndex(tem);
bindex = findIndex(tem2);
if(aindex==-1 or bindex == -1)
{
cout<<"F"<<endl;
continue;
}
if(ui[bindex/2] == ui[aindex])
{
printf("T\n");
}
else
printf("F\n");
}
else
{
int tem2=0,aindex,bindex;
int len = strlen(st);
for(int j=0;j<len;j++)
{
if(st[j]>='0' && st[j]<='9')
tem2 = tem2*10 + st[j]-'0';
}
aindex = findIndex(tem);
bindex = findIndex(tem2);
if(aindex==-1 or bindex == -1)
{
cout<<"F"<<endl;
continue;
}
if(ui[aindex/2]==ui[bindex])
printf("T\n");
else
printf("F\n");
}
}
return 0;
}
---