PAT (Level A) 2020 Fall Exam Questions and Answers

PAT (Level A) Fall 2020 Exam

1. 7-1 Panda and PP Milk (20 years)

Insert image description here
Insert image description here
The initial idea for this question was to find the lightest panda first, and then compare the weight with the pandas on both sides. If the weight is heavier, add 100 to the milk it drinks, otherwise it will remain unchanged. Then follow this method to traverse the pandas from small to large in weight. As a result, there are two test points 3 and 5 that cannot be passed. I don’t know where the mistake is.

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
    
    
	int weight, pos;
};
bool cmp(node a, node b)
{
    
    
	if(a.weight != b.weight)
	return a.weight < b.weight;
}
int main()
{
    
    
	vector<node> v;
	int n;
	scanf("%d", &n);
	vector<int> w(n), m(n, 0);
	for(int i = 0; i < n; i++)
	{
    
    
		scanf("%d", &w[i]);
		v.push_back({
    
    w[i], i});
	}
	sort(v.begin(), v.end(), cmp);
	for(int i = 0; i < n; i++)
	{
    
    
		int pos = v[i].pos;
		if(m[pos] == 0)
		m[pos] = 200;
		if(pos+1 < n)
		{
    
    
			if(w[pos] < w[pos+1])
			m[pos+1] = m[pos]+100;
			else if(w[pos] == w[pos+1])
			m[pos+1] = m[pos];
		}
		if(pos-1 >= 0)
		{
    
    
			if(w[pos] < w[pos-1])
			m[pos-1] = m[pos]+100;
			else if(w[pos] == w[pos-1])
			m[pos-1] = m[pos];
		}
	}
	int total = 0;
	for(int i = 0; i < n; i++)
	total += m[i];
	printf("%d", total);
	return 0;
}

After reading this blogger’s analysis, the method is simpler -> simpler method

2.7-2 How Many Ways to Buy a Piece of Land (25 分)(AC)

Insert image description here
Insert image description here
I didn’t think too much about this question and just used brute force to solve it:

#include<cstdio>
#include<vector>
using namespace std;
vector<int> v;
int n, m, cnt = 0;

int main()
{
    
    
	scanf("%d%d", &n, &m);
	v.resize(n);
	for(int i = 0; i < n; i++)
		scanf("%d", &v[i]);
	for(int i = 0; i < n; i++)
	{
    
    
		int total = v[i];
		if(total <= m)
		{
    
    
			cnt++;
			for(int j = i+1; j < n; j++)
			{
    
    
				if(total + v[j] <= m)
				{
    
    
					cnt++;
					total += v[j];
				}
				else
				break;
			}
		}
	}
	printf("%d", cnt);
}

Later, I saw a blogger's simpler approach: use sum[i] to represent the addition of the first i sums, and then use sum[i] to make the difference with the sum starting from k. If the difference at this time is greater than the given number, it means The money is not enough to buy the islands from k+1 to i, so we make a difference with sum[k+1] until we find a j that is smaller than the given number, that is, the given money can buy the continuous islands starting from j to i. And ji represents the method that can be chosen, sum[j+1] means only buying j+1, sum[j+2] means buying j+1~j+2...-> Blogger link

#include <iostream>
using namespace std;
const int N = 10010;
int n,m;
int sum[N];
int main(){
    
    
    cin >> n >> m;
    for(int i=1;i<=n;i++){
    
    
        int x;cin >> x;
        sum[i] = x + sum[i - 1];
    }
    int res = 0;
    for(int i=1,j=0;i<=n;i++)
    {
    
    
        while(sum[i] - sum[j] > m)
            j ++ ;
        res += i - j;//因为每一个sum代表前i项和,所以i-j表示可以选j+1, j+1~j+2,j+1~j+3......j+1~i共i-j个选择
    }
    cout << res << endl;
    return 0;
}


3.7-3 Left-View of Binary Tree (25 分)(AC)

Template question: Build the tree first, then traverse the levels, store the first one traversed at each level and then output it.

#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
vector<int> pre, in, depth[20];
struct node{
    
    
	int data, depth;
	node * left, * right;
};
node * root;
void create(node * &root, int prel, int prer, int inl, int inr)
{
    
    
	if(prel > prer)
	return;
	
	root = new node;
	root->data = pre[prel];
	root->left = root->right = NULL;
	
	int i = inl;
	while(in[i] != root->data)
	i++;
	int leftNum = i-inl;
	create(root->left, prel+1, prel+leftNum, inl, i-1);
	create(root->right, prel+leftNum+1, prer, i+1, inr);
}
void BFS(node * root)
{
    
    
	queue<node * > q;
	q.push(root);
	while(!q.empty())
	{
    
    
		node * front = q.front();
		q.pop();
		if(front->left != NULL)
		{
    
    
			q.push(front->left);
			front->left->depth = front->depth+1;
			depth[front->left->depth].push_back(front->left->data);
		}
		if(front->right != NULL)
		{
    
    
			q.push(front->right);
			front->right->depth = front->depth+1;
			depth[front->right->depth].push_back(front->right->data);
		}//保存每层的第一个结点
	}
}

int main()
{
    
    
	int n;
	scanf("%d", &n);
	pre.resize(n+1), in.resize(n+1);
	for(int i = 1; i <= n; i++)
	scanf("%d", &in[i]);
	for(int i = 1; i <= n; i++)
	scanf("%d", &pre[i]);
	
	create(root, 1, n, 1, n);
	root->depth = 1;
	depth[1].push_back(root->data);
	BFS(root);
	for(int i = 1; i < 20; i++)
	{
    
    
		if(depth[i].size() > 0)
		{
    
    
			if(i > 1)
			printf(" ");
			printf("%d", depth[i][0]);
		}
		else break;
	}
}

4.7-4 Professional Ability Test (30 分)

This question was so stinky and long that in the end I didn’t know what I was writing, and I didn’t fully understand the question. I just used the test sample to judge that OK means no loops, and Impossible means there is a loop, so I used it. BFS determines whether there is a ring. Find all points with an in-degree of 0 and put them into the queue, and then add the in-degree of the connected points -1 to the queue when they are dequeued. In this way, if there are n points that are dequeued in the end, it means that the topology can be formed, otherwise there will be a cycle. As for finding the shortest path, the DFS algorithm is used to find the minimum s and the maximum d and then save the paths passed along the way in ans. As a result, the last test point was overtime and 3 points were deducted.

#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
int n, m;
const int maxn = 1010;
vector<int> ind, ind2, que, link[maxn], temp, ans;
int score[maxn][maxn], value[maxn][maxn];
bool vis[maxn];
int mins, maxd;
queue<int> q;
bool BFS( )
{
    
    
	int cnt = 0;
	while(q.size() > 0)
	q.pop();
	for(int i = 0; i < n; i++)
	if(ind2[i] == 0)
	{
    
    
		q.push(i);
		vis[i] = true;
	}
	
	while(!q.empty())
	{
    
    
		int front = q.front();
		cnt++;
		q.pop();
		for(int i = 0; i < link[front].size(); i++)
		{
    
    
			int id = link[front][i];
			ind2[id]--;
			if(vis[id] == false && ind2[id] == 0)
			{
    
    
				q.push(id);
				vis[id] = true;
			}
		}
		
	}
	if(cnt == n)
	return true;
	else
	return false;
}
void DFS(int start, int des, int totals, int totald)
{
    
    
	if(start == des && totals > 0 && totald > 0)//防止刚开始进入就start = des而此时totals = 0, totald = 0导致跳出函数
	{
    
    
		if(totals < mins)
		{
    
    
			ans = temp;
			mins = totals;
			maxd = totald;
		}
		else if(totals == mins && totald > maxd)
		{
    
    
			ans = temp;
			maxd = totald;
		}
		return;
	}
	else if(totals > mins)	return;
	for(int i = 0; i < link[start].size(); i++)
	{
    
    
		int id = link[start][i];
		temp.push_back(id);
		DFS(id, des, totals+score[start][id], totald+value[start][id]);
		temp.pop_back();
	}
}
int main()
{
    
    
	scanf("%d%d", &n, &m);
	ind.resize(n, 0);
	for(int i = 0; i < m; i++)
	{
    
    
		int a, b;
		scanf("%d%d", &a, &b);
		scanf("%d%d", &score[a][b], &value[a][b]);
		ind[b]++;
		link[a].push_back(b);
	}
	ind2 = ind;
	
	int query, num;
	scanf("%d", &query);
	que.resize(query);
	
	int flag = 0;
	for(int i = 0; i < query; i++)
	scanf("%d", &que[i]);
	if(BFS())
	{
    
    
		printf("Okay.");
		flag = 1;
	}
	else
	printf("Impossible.");
	
	for(int i = 0; i < query; i++)
	{
    
    
		int id = que[i];
		if(ind[id] == 0)
		printf("\nYou may take test %d directly.", id);
		else if(flag == 1)
		{
    
    
			printf("\n");
			mins = 1000000000, maxd = -1;
			ans.clear(), temp.clear();
			for(int j = 0; j < n; j++)
			if(ind[j] == 0 &&j != id)
			{
    
    
				temp.push_back(j);
				DFS(j, id, 0, 0);
				temp.pop_back();
			}
			
			for(int k = 0; k < ans.size(); k++)
			{
    
    
				printf("%d", ans[k]);
				if(k < ans.size()-1) printf("->");
			}
		}
		else if(flag == 0)
		printf("\nError.");
	}
}

Guess you like

Origin blog.csdn.net/weixin_45486992/article/details/120167724