PAT基本数据结构题总结_队列,栈,链表,二叉树,并查集等

PAT基本数据结构题_队列,栈,链表,二叉树,并查集等

  1. leetcode与PAT的经典题目是互通的。
    <–>

1. 1104. Sum of Number Segments

题目描述

https://pintia.cn/problem-sets/994805342720868352/problems/994805363914686464

结题思路

思路1:三个for循环,第一个for循环遍历索引,第二个for循环控制长度,第三个for循环累加固定长度的数组的和。

  1. 第一个for循环遍历0到n-1。
  2. 第二个for循环控制长度0到n-i。
  3. 第三个for循环累加固定长度的数组的和。
  4. 将累加的和加起来。

思路2:两个for循环,一个for循环遍历个数,第二个for循环控制长度,但累加的和逐步的加到总sum中去。

  1. 第一层for循环i遍历n-1。
  2. 第二层for循环j遍历0到n-i-1。

思路3:统计出每一个数的规律:如设i为数组的索引,则数组中每一个数的累加次数为(i+1)(n-i),其累加值=(i+1)(n-i)*a[i]。

  1. 遍历此数组,利用公式计算累加和。

源代码

代码1:

#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <vector>
using namespace std;
int main()
{
	int n;
	double *a;
	cin >> n;
	double sum = 0;
	a = (double *)calloc(n, sizeof(double));
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n-i; j++)
		{	
			double tmpSum = 0; 
			for(int m = i; m <= i + j; m++)
			{
				tmpSum += a[m];
			}
			sum += tmpSum;
		}
	}
	cout << sum << endl;
	return 0;	
} 

代码2:

#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <vector>
using namespace std;
int main()
{
	int n;
	double *a;
	cin >> n;
	double sum = 0;
	a = (double *)calloc(n, sizeof(double));
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	for(int i = 0; i < n; i++)
	{
		double tmpSum = 0; 
		for(int j = i; j < n; j++)
		{	
			tmpSum += a[j];
			sum += tmpSum;
		}
	}
	cout << sum << endl;
	return 0;	
} 

代码3:

#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <vector>
using namespace std;
int main()
{
//	int n;
//	double * a;
//	double sum;
//	a = (double *)calloc(n, sizeof(double));
//	scanf("%d", &n);
    int n;
    scanf("%d", &n);
    double a[n];
    double sum = 0;
	for(int i = 0; i < n; i++)
	{
		scanf("%lf", &a[i]);
		sum += a[i]*(i+1)*(n-i);
	}
	printf("%.2f", sum);
	return 0;
} 

题型分析

  1. 模拟法一般都超时,用找规律统计出现次数的方法解决。

下次再此类题中要注意的地方,通过这道题,我学到了什么新东西吗

  1. (double *)alloc(n, sizeof(double))分配动态内存。
  2. 数组的累加操作:三层

此类题模板代码

2. 1004 Counting Leaves

题目描述

https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184

结题思路

思路1:首先要看懂题目的输入方式:第一行表示有N个结点,M个非叶子结点。接下来的M行表示M个非叶子结点的描述:首先是此节点的ID,其孩子数量,孩子的ID。之后,题意就是计算此树的每一层非叶子结点的数量。使用递归(DFS)的方法解决此问题。

  1. 使用vector数组存储每个结点及其孩子,用deepth记录树的深度。
  2. 调用dfs()函数,遍历vector数组的每个元素,若其size==0,则其为叶子结点,num[deep]++;否则递归调用dfs(),传入此元素与深度。

源代码

#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <vector>
using namespace std;
vector<int> a[100];
int res[100];
int maxDepth = 0;
int count = 0;
void dfs(int i, int depth)
{
	// DFS进行深度优先遍历时,因为是深度的,所以可能每一层的depth都不一样 
	cout << count++ << endl;
	maxDepth = max(depth, maxDepth);
	// a[i]的孩子数量 
	int len = a[i].size();
	if(len == 0)
	{
		res[depth]++; 
	}
	else
	{
		// vector数组的意思是:vector是一个数组,然后这个数组中的每一个元素又是一个容器,类似于一个数组 
		for(int j = 0; j < a[i].size(); j++)
		{
			//at(i)函数取出vector中的第j个元素 
			dfs(a[i].at(j), depth+1);
		}
	}
}
int main()
{
	int n, m;
	int id, num;
	int depth = 0;
	int tmp;
	cin >> n >> m;
	for(int i = 0; i < m; i++)
	{
		cin >> id;
		cin >> num; 
		for(int j = 0; j < num; j++)
		{
			cin >> tmp;
			a[i].push_back(tmp);
		}
	}
	dfs(1, depth);
	cout << "maxDepth:" << maxDepth << endl;
	for(int i = 0; i < maxDepth; i++)
	{
		cout << res[i] << " ";
	}
	return 0;
} 

题型分析

  1. DFS题,要熟悉vector容器数组的常见用法与函数。

下次再此类题中要注意的地方,通过这道题,我学到了什么新东西吗

1. DFS题搜索树型题时,在同一个dfs函数中调用dfs,不能depth++,只能depth+1。2. vector的.at(i)的用法。

此类题模板代码

if(len == 0)
	{
		res[depth]++; 
	}
	else
	{
		// vector数组的意思是:vector是一个数组,然后这个数组中的每一个元素又是一个容器,类似于一个数组 
		for(int j = 0; j < a[i].size(); j++)
		{
			//at(i)函数取出vector中的第j个元素 
			dfs(a[i].at(j), depth+1);
		}
	}

3. 1020 Tree Traversals

题目描述

https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072

结题思路

DFS的前序、中序与后序遍历与BFS总结图:

思路1:
1.

源代码

题型分析

下次再此类题中要注意的地方,通过这道题,我学到了什么新东西吗

此类题模板代码

leetcode与PAT的经典题目是互通的。

题目描述

结题思路

源代码

题型分析

下次再此类题中要注意的地方,通过这道题,我学到了什么新东西吗

此类题模板代码

题目描述

结题思路

源代码

题型分析

下次再此类题中要注意的地方,通过这道题,我学到了什么新东西吗

此类题模板代码

题目描述

结题思路

源代码

题型分析

下次再此类题中要注意的地方,通过这道题,我学到了什么新东西吗

此类题模板代码

题目描述

结题思路

源代码

题型分析

下次再此类题中要注意的地方,通过这道题,我学到了什么新东西吗

此类题模板代码

题目描述

结题思路

源代码

题型分析

下次再此类题中要注意的地方,通过这道题,我学到了什么新东西吗

此类题模板代码

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值