基础实验4-2.5-关于堆的判断-编程题

基础实验4-2.5-关于堆的判断-编程题

解题代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXN 1000
#define MAXM 20
#define MINX -10001
typedef enum { false, true } bool;
typedef struct heap* ph;
struct heap {
	int data[MAXN];
	int index;
};
ph Insert(ph H, int temp_x);
bool IsSiblings(ph H, int temp_x, int temp_x2);
bool IsChild(ph H, int temp_x2, int temp_x);
bool IsRoot(ph H, int temp_x);
bool IsParent(ph H, int temp_x, int temp_x2);
ph CreatMinHeap(int temp_x);
int Peek(ph H, int x);
int main()
{
	int N, M, i, temp_x, temp_x2;//N : n of x; M : n of case
	ph H = NULL;
	scanf("%d %d", &N, &M);
	for (i = 0; i < N; i++) {
		scanf("%d", &temp_x);
		H = Insert(H, temp_x);
	}
	char temp_case[101];
	for (i = 0; i < M; i++) {
		scanf("%d ", &temp_x);
		scanf("%s ", &temp_case);
		if (!strcmp(temp_case, "and")) {
			scanf("%d", &temp_x2);
			if (IsSiblings(H, temp_x, temp_x2)) printf("T\n");
			else printf("F\n");
			scanf("%s ", &temp_case);
			scanf("%s", &temp_case);
		}
		else {
			scanf("%s ", &temp_case);
			if (!strcmp(temp_case, "a")) {
				scanf("%s ", &temp_case);
				scanf("%s ", &temp_case);
				scanf("%d", &temp_x2);
				if(IsChild(H, temp_x2, temp_x)) printf("T\n");//x2 : dad ; x : child ;
				else printf("F\n");
			}
			else {
				scanf("%s ", &temp_case);
				if (!strcmp(temp_case, "root")) {
					if (IsRoot(H, temp_x)) printf("T\n");
					else printf("F\n");
				}
				else {
					scanf("%s ", &temp_case);
					scanf("%d ", &temp_x2);
					if(IsParent(H,temp_x, temp_x2)) printf("T\n");//x : dad ; x2 : child
					else printf("F\n");
				}
			}
		}
	}
	return 0;
}
ph Insert(ph H, int temp_x) {
	if (!H) return CreatMinHeap(temp_x);
	int i;
	for (i = ++H->index; H->data[i / 2] > temp_x; i /= 2) H->data[i] = H->data[i / 2];
	H->data[i] = temp_x;
	return H;
}
bool IsSiblings(ph H, int temp_x,int temp_x2) {
	if (Peek(H, temp_x) == 1 || Peek(H, temp_x2) == 1) return false;
	if ((!(Peek(H, temp_x) % 2) && Peek(H, temp_x2) == Peek(H, temp_x) + 1) || (!(Peek(H, temp_x2) % 2) && Peek(H, temp_x) == Peek(H, temp_x2) + 1)) return true;
	else return false;
}
bool IsChild(ph H, int dad,int son) {//x2 : dad ; x : child ;
	if (Peek(H, son) / 2 == Peek(H, dad)) return true;
	else return false;
}
bool IsRoot(ph H,int x) {
	return x == H->data[1] ? true : false;
}
bool IsParent(ph H, int dad,int son) {//x : dad ; x2 : child
	if (Peek(H, son) / 2 == Peek(H, dad)) return true;
	else return false;
}
ph CreatMinHeap(int temp_x) {
	ph H = (ph)malloc(sizeof(struct heap));
	H->index = 0;
	H->data[0] = MINX;
	H->data[++H->index] = temp_x;
	return H;
}
int Peek(ph H,int x) {
	int index = 0;
	while (H->data[++index] != x) ;
	return index;
}

测试结果

在这里插入图片描述

问题整理

1.不要上午码代码。。。写出了漏洞百出的程序。
2.主程序不要太大。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值