2019年秋PAT总结

总览判断,偶尔看看题榜,决定做题顺序。

一定要认真审题,注意细节,比如no的时候输出什么……

思路清晰再下笔。

很烂的结果,往往隐藏着最正确的答案。

7-1 Forever 未作答 得分: 0 / 20

"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

暴力发现规律,固定两位,分数其实很好拿。

#include<bits/stdc++.h>
using namespace std;
struct node {
	int a,b;
};
vector<node> ans;
int n,k,m,flag;
int gcd(int a,int b) {
	return b==0?a:gcd(b,a%b);
}
bool isprime(int a) {
	if(a<2) return false;
	int maxn=sqrt(a);
	for(int i=2; i<=maxn; i++)
		if(a%i==0) return false;
	return true;
}
int cal(int t) {
	int sum=0;
	while(t) {
		sum+=t%10;
		t/=10;
	}
	return sum;
}
bool ok(int num) {
	int sum1=0,sum2=0,t=num;
	sum1=cal(num);
	if(sum1!=m) return false;
	sum2=cal(num+1);
	int lca=gcd(sum1,sum2);
	if(isprime(lca)) return true;
	else return false;
}
void dfs(int num,int cnt) {
	if(cnt==k) {
		if(ok(num)) ans.push_back(node {cal(num+1),num});
		return;
	}
	for(int i=0; i<=9; i++)
		dfs(num*10+i,cnt+1);
}
int main() {
	scanf("%d",&n);
	for(int i=1; i<=n; i++) {
		ans.clear();
		flag=0;
		printf("Case %d\n",i);
		scanf("%d%d",&k,&m);
		for(int i=1; i<=9; i++) {
			dfs(i,1);
		}
		if(ans.size()==0) {
			printf("No Solution\n");
			continue;
		}
		
		for(int i=0; i<ans.size(); i++)
			printf("%d %d\n",ans[i].a,ans[i].b);
	}
	return 0;
}


7-2 Merging Linked Lists 答案正确 得分: 25 / 25

Given two singly linked lists L1=a1→a2→⋯→an−1→anL_1 = a_1 \to a_2\to \cdots \to a_{n-1}\to a_nL​1​​=a​1​​→a​2​​→⋯→a​n−1​​→a​n​​ and L2=b1→b2→⋯→bm−1→bmL_2 = b_1 \to b_2\to \cdots \to b_{m-1}\to b_mL​2​​=b​1​​→b​2​​→⋯→b​m−1​​→b​m​​. If n≥2mn\ge 2mn≥2m, you are supposed to reverse and merge the shorter one into the longer one to obtain a list like a1→a2→bm→a3→a4→bm−1⋯a_1 \to a_2 \to b_{m} \to a_3 \to a_4 \to b_{m-1}\cdots a​1​​→a​2​​→b​m​​→a​3​​→a​4​​→b​m−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 L1L_1L​1​​ and L2L_2L​2​​, plus a positive NNN (≤105\le 10^5≤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 NNN 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 10510^510​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

#include<bits/stdc++.h>
using namespace std;
struct node{
	int id,next;
	int data;
}p[100005];
vector<node> v1,v2,tmp,ans;
int pos[100005];
int main(){
	int n;
	int s1,s2;
	cin>>s1>>s2>>n;
	for(int i=0;i<n;i++){
		cin>>p[i].id>>p[i].data>>p[i].next;
		pos[p[i].id]=i; 
	}
	int t1=s1,t2=s2;
	while(t1!=-1){
		v1.push_back(p[pos[t1]]);
		t1=p[pos[t1]].next;	
	}
	while(t2!=-1){
		v2.push_back(p[pos[t2]]);
		t2=p[pos[t2]].next;
	}
	if(v1.size()<v2.size()) swap(v1,v2);
	int i=0,j=v2.size()-1;
	for(i=0;i<v1.size();i++){
		ans.push_back(v1[i]);
		if((i+1)%2==0&&j>=0) //短的可能更短 第一次做在此处扣分
			ans.push_back(v2[j--]);
	}
	for(int i=0;i<ans.size()-1;i++)
		printf("%05d %d %05d\n",ans[i].id,ans[i].data,ans[i+1].id);
	printf("%05d %d -1\n",ans[ans.size()-1].id,ans[ans.size()-1].data);	
	return 0;
} 

 
7-3 Postfix Expression 答案正确 得分: 25 / 25

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.

infix1.JPGinfix2.JPG
Figure 1Figure 2

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)+)((c)(-(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

 不会左右子树的输出情况就看样例,多试试 

#include<bits/stdc++.h>
using namespace std;
struct node {
	string data;
	int l,r;
} p[25];
int have[25];
int n;
string dfs(int i) {
	if(p[i].l==-1&&p[i].r==-1) return "("+p[i].data+")";
	if(p[i].l==-1&&p[i].r!=-1) return "("+p[i].data+dfs(p[i].r)+")";
	if(p[i].l!=-1&&p[i].r!=-1) return "("+dfs(p[i].l)+dfs(p[i].r)+p[i].data+")";
}
int main() {
	int a,b;
	cin>>n;
	for(int i=1; i<=n; i++) {
		cin>>p[i].data>>p[i].l>>p[i].r;
		if(p[i].l!=-1) have[p[i].l]=1;
		if(p[i].r!=-1) have[p[i].r]=1;
	}
	int root=1;
	while(have[root]) root++;
 	cout<<dfs(root);
	return 0;
}


7-4 Dijkstra Sequence 答案正确 得分: 30 / 30

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 N​v​​ (≤10​3​​) and N​e​​ (≤10​5​​), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to N​v​​.

Then N​e​​ 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 N​v​​ 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

还是不太会变通,替换等值边

判断是否找到点,是的话能替换吗,换过了吗?

#include<bits/stdc++.h>
using namespace std;
int mp[1005][1005];
int vis[1005],num[1005],dis[1005];
const int Inf=0x3f3f3f3f;
int n;
bool dijstra(int id) {
	memset(vis,0,sizeof vis);
	memset(dis,Inf,sizeof dis);
	vis[id]=1;
	dis[id]=0;
	for(int i=1;i<=n;i++) dis[i]=min(dis[i],mp[id][i]);
	for(int i=1; i<n; i++) {
		int minn=Inf,u=-1;
		for(int j=1; j<=n; j++) {
			if(vis[j]==0) {
				if(dis[j]<minn) {
					minn=dis[j];
					u=j;
				}
			}
		}
		if(u==-1||dis[u]!=dis[num[i]]||vis[num[i]==1]) return false;
		u=num[i];
		vis[u]=1;
		//cout<<"u: "<<u<<endl;
		for(int j=1; j<=n; j++) {
			if(vis[j]==0&&mp[u][j]!=Inf)
				if(dis[j]>dis[u]+mp[u][j])
					dis[j]=dis[u]+mp[u][j];
		}
	}
	return true;
}
int main() {
	int m,a,b,d,k;
	scanf("%d %d",&n,&m);
	memset(mp,Inf,sizeof mp);
	for(int i=0; i<m; i++) {
		scanf("%d%d%d",&a,&b,&d);
		mp[a][b]=mp[b][a]=d;
	}
	cin>>k;
	while(k--) {
		for(int i=0; i<n; i++)
			scanf("%d",&num[i]);

		if(dijstra(num[0])) printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值