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的經典題目是互通的。

題目描述

結題思路

源代碼

題型分析

下次再此類題中要注意的地方,通過這道題,我學到了什麼新東西嗎

此類題模板代碼

題目描述

結題思路

源代碼

題型分析

下次再此類題中要注意的地方,通過這道題,我學到了什麼新東西嗎

此類題模板代碼

題目描述

結題思路

源代碼

題型分析

下次再此類題中要注意的地方,通過這道題,我學到了什麼新東西嗎

此類題模板代碼

題目描述

結題思路

源代碼

題型分析

下次再此類題中要注意的地方,通過這道題,我學到了什麼新東西嗎

此類題模板代碼

題目描述

結題思路

源代碼

題型分析

下次再此類題中要注意的地方,通過這道題,我學到了什麼新東西嗎

此類題模板代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章