L2-012 关于堆的判断 (25 分)

博客围绕关于堆的判断相关内容,虽未给出具体内容,但推测是信息技术领域中数据结构里堆相关的判断问题,可能涉及堆的性质、操作等方面的判断。
#include<iostream>
#include<vector>
#include<set>
#include<string>
#include<algorithm>
#include<string.h>
#include<sstream>
#define iIN(x,y) for(int i=x;i<y;i++) 
using namespace std;
int heap[1005];
int N,M;
void adjust(){ //建堆:从第零个开始调整 
	iIN(1,N+1){
		int now=i;
		while(now!=1){
			if(heap[now/2]>heap[now]){ //此结点比父节点小(小顶堆父节点应是最小的)
				int box=heap[now/2];
				heap[now/2]=heap[now];
				heap[now]=box;
			}
			else break;
			now/=2;//接着就调整父节点 
		}
	}
}
int getlocate(int num){
	iIN(1,N+1){
		if(heap[i]==num)return i; 
	} 
}
int sTOint(string s){
	stringstream water;
	water<<s;
	int box;
	water>>box;
	return box;
}
string s[10];
int main(){
	cin>>N>>M;
	iIN(1,N+1){
		cin>>heap[i];
	}
	adjust();
	string st;
	getchar();
	while(M--){
		getline(cin,st);	
		stringstream water(st);
		int i=0;
		while(water>>s[i++]);
		//以下处理s数组即可
		if(s[3]=="root"){
			//s[0] 是根节点?
			if(heap[1]==sTOint(s[0]))cout<<"T"<<endl;
			else cout<<"F"<<endl;			
		}	
		else if(s[3]=="are"){
			//s[0] s[2]是兄弟结点
			int n1=getlocate(sTOint(s[0]));
			int n2=getlocate(sTOint(s[2]));
			if(n1/2==n2/2)cout<<"T"<<endl;
			else cout<<"F"<<endl;
		}
		else if(s[3]=="parent"){
			//s[0]是s[5]的父节点
			if(getlocate(sTOint(s[5]))/2==getlocate(sTOint(s[0])))cout<<"T"<<endl;
			else cout<<"F"<<endl; 
			
		}
		else{
			if(getlocate(sTOint(s[0]))/2==getlocate(sTOint(s[5])))cout<<"T"<<endl;
			else cout<<"F"<<endl;
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会写代码的孙悟空

赠人玫瑰 手有余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值