其他分享
首页 > 其他分享> > PAT2019-秋季

PAT2019-秋季

作者:互联网

19.9.8周日下午13:30-16:45

发挥不好。把题目重新做一遍。

 

第一题

7-1 Forever
题目:
“Forever number” is a positive integer A with K digits, satisfying the following constrains:

the sum of all the digits of A is m;
the sum of all the digits of A+1 is n; and
the greatest common divisor of m and n is a prime number which is greater than 2.
Now you are supposed to find these forever numbers.

Input Specification:
Each input file contains one test case. For each test case, the first line contains a positive integer N (≤5). Then N lines follow, each gives a pair of K (3<K<10) and m (1<m<90), of which the meanings are given in the problem description.

Output Specification:
For each pair of K and m, first print in a line Case X, where X is the case index (starts from 1). Then print n and A in the following line. The numbers must be separated by a space. If the solution is not unique, output in the ascending order of n. If still not unique, output in the ascending order of A. If there is no solution, output No Solution.

Sample Input:
2
6 45
7 80

Sample Output:
Case 1
10 189999
10 279999
10 369999
10 459999
10 549999
10 639999
10 729999
10 819999
10 909999
Case 2
No Solution

这题坑最多。

AC代码

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <set>
#include <vector>
#include <map>
using namespace std;

//思路,来自某大神考完的分享
//考虑m的形状。
//若m的个位数不是9,则n=m+1,则gcd必然为1,不符合大于2素数的要求。
//从而m的个位必然为9。
//若m的十位数不是9,而个位9,则n=m-8
//百位非9,个十为9,则n=m-17
//以此类推,n=m+1-t*9,t为末位连续9的个数。
//找出满足isprime的t,然后逐次找出对应A即可。
//看讨论,大部分人好像是用dfs做的。这个思路比较出彩。

bool isprime(int a)
{
	if (a <= 2) return false; //题目要求大于2的素数,所以判2为不合法
	int sqr = sqrt(1.0*a);
	for (int i = 2; i <= sqr; i++) if (a%i == 0) return false;
	return true;
}

int get_sum(int x) {
	int summ = 0;
	while (x) {
		summ += x % 10;
		x /= 10;
	}
	return summ;
}

int gcd(int a, int b)
{
	if (b == 0) return a;
	return gcd(b, a%b);
}

int main()
{
	int N, k, m, n, t;
	cin >> N;
	for (int i = 1; i <= N; i++)
	{	
		scanf("%d%d", &k, &m);
		int minn=pow(10.0,k-1), maxn = pow(10.0, k), addition, zf = 1;
		printf("Case %d\n", i);

		//已知个位数必然为9,寻找合法的t值
		for (t = 1; t < k; ++t) {
			n = m + 1 - t * 9;
			if (!isprime(gcd(m, n))) continue;
			//对合法的t值进行操作
			minn += pow(10.0, t) - 1 ;
			addition = pow(10.0, t);
			for (int j = minn, cnt = 0; j < maxn; j += addition) {
				if (cnt % 10 == 9) { cnt++; continue; } //限定9的位数
				if (get_sum(j) == m) { zf = 0; printf("%d %d\n", n, j); }
				cnt++;
			}
		}
		//若找不到
		if (zf) { printf("No Solution"); }
	}

}

 

第二题

7-2 Merging Linked Lists
题目:
Given two singly linked lists L1=a1→a2→⋯→an−1→an and L2=b1→b2→⋯→bm−1→b​m.If n≥2m, you are supposed to reverse and merge the shorter one into the longer one to obtain a list like a1→a2→b​m→a3→a4→bm−1⋯. For example, given one list being 6→7 and the other one 1→2→3→4→5, you must output 1→2→7→3→4→6→5.

Input Specification:
Each input file contains one test case. For each case, the first line contains the two addresses of the first nodes of L1and L2, plus a positive N (≤10^​5) which is the total number of nodes given. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Data Next
where Address is the position of the node, Data is a positive integer no more than 10^​5
​​ , and Next is the position of the next node. It is guaranteed that no list is empty, and the longer list is at least twice as long as the shorter one.

Output Specification:
For each case, output in order the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:
00100 01000 7
02233 2 34891
00100 6 00001
34891 3 10086
01000 1 02233
00033 5 -1
10086 4 00033
00001 7 -1

Sample Output:
01000 1 02233
02233 2 00001
00001 7 34891
34891 3 10086
10086 4 00100
00100 6 00033
00033 5 -1

签到题难度,AC代码

#include<iostream>
#include<vector>
using namespace std;
struct Node {
	int address, data, next;
} node[100001];
int main()
{
	int head1, head2, n, address, data, next;
	scanf("%d%d%d", &head1, &head2, &n);
	for (int i = 0; i < n; i++) {
		scanf("%d%d%d", &address, &data, &next);
		node[address] = { address, data, next };
	}
	vector<Node> list1, list2, result;
	for (int p = head1; p != -1; p = node[p].next)
		list1.push_back(node[p]);
	for (int p = head2; p != -1; p = node[p].next)
		list2.push_back(node[p]);
	if (list1.size() > list2.size()) {
		int j = list2.size() - 1;
		for (int i = 0; i < list1.size(); i = i + 2) {
			result.push_back(list1[i]);
			if (i + 1 < list1.size()) result.push_back(list1[i + 1]);
			if (j >= 0) result.push_back(list2[j--]);
		}
	}
	else {
		int j = list1.size() - 1;
		for (int i = 0; i < list2.size(); i = i + 2) {
			result.push_back(list2[i]);
			if (i + 1 < list2.size()) result.push_back(list2[i + 1]);
			if (j >= 0) result.push_back(list1[j--]);
		}
	}
	for (int i = 0; i + 1 < result.size(); i++)
		printf("%05d %d %05d\n", result[i].address, result[i].data, result[i + 1].address);
	printf("%05d %d -1\n", result[result.size() - 1].address, result[result.size() - 1].data);
	return 0;
}
//https://www.cnblogs.com/whale90830/p/11494161.html

 

 

第三题

7-3 Postfix Expression

 

figure1figure2
题目:
Given a syntax tree (binary), you are supposed to output the corresponding postfix expression, with parentheses reflecting the precedences of the operators.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 20) which is the total number of nodes in the syntax tree. Then N lines follow, each gives the information of a node (the i-th line corresponds to the i-th node) in the format:

data left_child right_child
where data is a string of no more than 10 characters, left_child and right_child are the indices of this node’s left and right children, respectively. The nodes are indexed from 1 to N. The NULL link is represented by −1. The figures 1 and 2 correspond to the samples 1 and 2, respectively.

Output Specification:
For each case, print in a line the postfix expression, with parentheses reflecting the precedences of the operators.There must be no space between any symbols.

Sample Input 1:
8

8 7
a -1 -1
4 1
2 5
b -1 -1
d -1 -1
-1 6
c -1 -1
Sample Output 1:
(((a)(b)+)(©(-(d))))

Sample Input 2:
8
2.35 -1 -1

6 1
-1 4
% 7 8
2 3
a -1 -1
str -1 -1
871 -1 -1
Sample Output 2:
(((a)(2.35)*)(-((str)(871)%))+)
 

这题不难,就是最后一个测试点死活过不去。

不知道问题出在哪。出来讨论发现很多人也是卡在最后一个点。

有些人不知道怎么就过了。

我一开始用的方案是重新建一颗标准树。不过好像不必这么麻烦。

下面这个方案是别人的思路。

细节在于,他只对左孩空右孩非空时,先输出根,再访问右孩。

而我在考场写的特判条件是,【根值=='-' 且 左右孩仅有1个非空 】时,先输出根,若左孩非空,访问左,若右孩非空访问右。

博主提到可能存在正负号±,不能只考虑负号。晕了,原地取正是什么操作。

别人的AC代码

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <set>
#include <vector>
#include <map>
using namespace std;

struct Node {
	string data;
	int lchild, rchild;
} node[21];
bool occured[21] = { false };
void postorder(int root) {
	cout << "(";
	if (node[root].lchild == -1 && node[root].rchild != -1) {
		cout << node[root].data;
		postorder(node[root].rchild);
		cout << ")";
	}
	else {
		if (node[root].lchild != -1) postorder(node[root].lchild);
		if (node[root].rchild != -1) postorder(node[root].rchild);
		cout << node[root].data << ")";
	}
}
int main()
{
	int n, root;
	string data;
	scanf("%d", &n);
	for (int i = 1; i < n + 1; i++) {
		cin >> node[i].data >> node[i].lchild >> node[i].rchild;
		if (node[i].lchild != -1) occured[node[i].lchild] = true;
		if (node[i].rchild != -1) occured[node[i].rchild] = true;
	}
	for (root = 1; root < n + 1 && occured[root]; root++);
	postorder(root);
	return 0;
}
//https://www.cnblogs.com/whale90830/p/11494161.html

 

 

第四题

7-4 Dijkstra Sequence
题目:

Dijkstra’s algorithm is one of the very famous greedy algorithms. It is used for solving the single source shortest path problem which gives the shortest paths from one particular source vertex to all the other vertices of the given graph. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.

In this algorithm, a set contains vertices included in shortest path tree is maintained. During each step, we find one vertex which is not yet included and has a minimum distance from the source, and collect it into the set. Hence step by step an ordered sequence of vertices, let’s call it Dijkstra sequence, is generated by Dijkstra’s algorithm.

On the other hand, for a given graph, there could be more than one Dijkstra sequence. For example, both { 5, 1, 3, 4, 2 } and { 5, 3, 1, 2, 4 } are Dijkstra sequences for the graph, where 5 is the source. Your job is to check whether a given sequence is Dijkstra sequence or not.

Input Specification:
Each input file contains one test case. For each case, the first line contains two positive integers Nv(≤10^3) and Ne(≤10 ^5), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to Nv. Then Ne lines follow, each describes an edge by giving the indices of the vertices at the two ends, followed by a positive integer weight (≤100) of the edge. It is guaranteed that the given graph is connected.
Finally the number of queries, K, is given as a positive integer no larger than 100, followed by K lines of sequences, each contains a permutationof the Nv vertices. It is assumed that the first vertex is the source for each sequence.
All the inputs in a line are separated by a space.

Output Specification:
For each of the K sequences, print in a line Yes if it is a Dijkstra sequence, or No if not.

Sample Input:
5 7
1 2 2
1 5 1
2 3 1
2 4 1
2 5 2
3 5 1
3 4 1
4
5 1 3 4 2
5 3 1 2 4
2 3 4 5 1
3 2 1 5 4

Sample Output:
Yes
Yes
Yes
No

 

谁能想到最后一题居然是最简单的题。

而第一个20分题居然是坑最多的题。

如果顺序颠倒过来做就好了。

 

AC代码

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <set>
#include <vector>
#include <map>
using namespace std;

//思路
//还是dij,每次取出最短的结点u后,比较d[u]==d[query],若相等则u=query。若不等则返回false。

const int INF = 0x3fffffff;
int n, G[1001][1001], d[1001], query[1001];
bool Dijkstra(int root){
    fill(d, d+1001, INF);
    bool vis[1001] = {false};
    d[root] = 0;
    for (int i = 0; i < n; i++){
        int u, min = INF;
        for (int j = 1; j < n + 1; j++)
            if (d[j] < min && !vis[j])
                min = d[j];
        if (d[query[i]] == min) u = query[i];
        else return false;
        vis[u] = true;
        for (int j = 1; j < n + 1; j++)
            if (G[u][j] && !vis[j] && d[j] > d[u] + G[u][j])
                d[j] = d[u] + G[u][j];
    }
    return true;
}

int main()
{
    int m, u, v, distance, k;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; i++){
        scanf("%d%d%d", &u, &v, &distance);
        G[u][v] = G[v][u] = distance;
    }
    scanf("%d",&k);
    for (int i = 0; i < k; i++){
        for (int j = 0; j < n; j++) scanf("%d", &query[j]);
        bool isD = Dijkstra(query[0]);
        printf("%s\n", isD ? "Yes" : "No");
    }
    return 0;
}

 

 

 

 

参考://https://blog.csdn.net/weixin_44742521/article/details/100752614

标签:秋季,node,PAT2019,int,Dijkstra,result,each,include
来源: https://blog.csdn.net/w55100/article/details/100694975