二叉树的层级遍历以及[NOIP2015 普及组] 扫雷游戏、有效时间的数目

一、二叉树的层级遍历

二叉树的层级遍历看着比其他遍历简单,但是我感觉实施起来却比其他遍历难,它主要是通过队列实现的

比如在这样的一颗二叉树中

我没先将a入队        队列:a

当a出队的时候就将它的左儿子和右儿子入队        队列:b   f

然后将b出队,将b的左儿子和右儿子入队        队列:f   c   d

然后将f出队,将f的左儿子和右儿子入队        队列:c   d   g

依次类推,当出队完之后这就是一个层级遍历

代码实现  

#include<stdio.h>

#include<stdlib.h>

#define MAX 1000

int max = 0;



typedef struct tree

{

char date;  //存放数据

struct tree* left;   //左儿子

struct tree* right;  //右儿子

}tree, * bittree;



typedef struct

{

bittree base[MAX];  //基地址

int front;  //头指针

int rear;  //尾指针

}sqqueue;



bittree creat()

{

char x, y;

bittree s;

scanf_s("%c", &x);

y = getchar();

if (x == '-')s = NULL;

else

{

s = (bittree)malloc(sizeof(tree));

s->date = x;

printf("请输入%c的左结点:", s->date);

s->left = creat();

printf("请输入%c的右结点:", s->date);

s->right = creat();

}

return s;

}



void enqueue(sqqueue* q,bittree s)

{

if ((q->rear + 1) % MAX == q->front)printf("队已满");

else

{

q->base[q->rear] = s;

q->rear = (q->rear + 1) % MAX;

}

}



void dequeue(sqqueue* s)

{

if (s->front == s->rear)printf("队列为空");

else

{

printf("%c ", s->base[s->front]->date);

s->front = (s->front + 1) % MAX;

}

}



int cenorder(bittree s)

{

int num=0;

sqqueue q;

q.front = q.rear = 0;

bittree temp;

enqueue(&q,s);

while (1)

{

if (q.front == q.rear)break;

temp = q.base[q.front];

dequeue(&q);

if (temp->left != NULL)enqueue(&q, temp->left);

if (temp->right != NULL)enqueue(&q, temp->right);

if (temp->left == NULL && temp->right != NULL || temp->left != NULL && temp->right == NULL)num++;

}

return num;

}



int main()

{

int num;

bittree s;

printf("请输入第一个结点:");

s = creat();

printf("层级遍历为:");

num = cenorder(s);

printf("\n度为1的结点有%d个", num);

}

二、2437. 有效时有效时间的数目2437. 有效时

思路

这道题没什么好说的,存粹的逻辑题,这是你要注意一下该怎么判断,哪个点应该怎么做,我是通过一个个字母是否等于 ' ? ' 推出的;

代码实现

int countTime(char * time){
    int num=1;
    if(time[0]=='?'&& time[1]=='?')num=24;
    else if(time[0]=='?'&&time[1]<'4')num=3;
    else if(time[0]=='?'&&time[1]>='4')num=2;
    else if(time[1]=='?'&&time[0]=='2')num=4;
    else if(time[1]=='?'&&time[0]<'2')num=10;
    if(time[3]=='?'&&time[4]=='?')num*=60;
    else if(time[3]=='?'&&time[4]>='0')num*=6;
    else if(time[4]=='?'&&time[3]=='6')num*=1;
    else if(time[4]=='?'&&time[3]<'6')num*=10;
    return num;
}

三、[NOIP2015 普及组] 扫雷游戏

 

思路 

这道题就是统计一下 ' * ' 的8个方向到底有多少个 ' ? ' ,就这么简单,我觉得难的是字符串输入.......,因为是二维数组,每行输入的时候还要把换行输进去,所以就很麻烦,但是还是做出来了,不过挺狗血的

还有就是一点你答案的换行要输出正确,不然也是不对的.........

代码实现

#include<stdio.h>
int n, m;
int dx[8] = { 0,0,1,-1,1,1,-1,-1 };
int dy[8] = { 1,-1,0,0,1,-1,-1,1 };

int main()
{
	int i, j;
	char a = '0';
	scanf("%d %d", &n, &m);
	char dis[101][101];
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < m; j++)
		{
			scanf("%c", &dis[i][j]);
			if (dis[i][j] == '\n')scanf("%c", &dis[i][j]);
		}
	}
	for (i = 0; i < n; i++)for (j = 0; j < m; j++)
	{
		a = '0';
		if (dis[i][j] == '?')
		{
			for (int k = 0; k < 8; k++)
			{
				int xx = i + dx[k];
				int yy = j + dy[k];
				if (xx >= 0 && xx < n && yy >= 0 && yy < m){
					if (dis[xx][yy] == '*')a++;
				}
			}
			dis[i][j] = a;
		}
	}
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < m; j++)printf("%c", dis[i][j]);
		printf("\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值