PAT(甲级)2019年秋季考试 结题报告

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

参考:

真希望不要碰上这种题-,-

参考:https://blog.csdn.net/Morizunzhu/article/details/100660334

           https://blog.csdn.net/TheWise_lzy/article/details/100660036

          https://blog.csdn.net/qq_24452475/article/details/100674030?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

分析:

方法1

除了最高位是1-9,剩余各个位都是0-9.那么从最高位往最低递归放置1(0)-9后计算是否满足。
       在进入递归后,判断当前位数和是否大于m或者当前位数和在剩下位数即使都是9的情况下也不能满足总和大于等于m那么退出本层递归。

从递归的代码里可以学到一些小技巧。

 方法2

打表,通过打表观察规律

这里是打表m,n来找规律

m最大值: 89,n最大值: 90

  • 计算90以内m、n公共最大除数,该除数是大于2的质数
    • m+1必定存在进位,否则无结果
    • 条件转化1: 进位等价于计算m以多少个数字9结尾
      • 条件转化2: x个9结尾,则有(m-n+1)%9==0
        • 最小差值: 17,即以99结尾

也就是说打表得到m,n最小差值是17,ans必定以99结尾。

然后就可以通过这个结论,结合ans的和为m,就可以降低时间复杂度

这道题暴力超时,考虑dfs剪枝or规律

代码:

#include<bits/stdc++.h>
using namespace std;
//注意,完全可以用vector<pair<int,int>> v;来代替! 
struct record {
    int sum, val;
    record(int v, int n) : val(v), sum(n) {}
    bool operator<(record &x) {
        if (sum != x.sum) return sum < x.sum;
        else return val < x.val;
    }
};
vector<record> r; 
bool isPrime(int x){
	if(x<2) return false;
	for(int i=2;i<=sqrt(x);i++){
		if(x%i==0) return false;
	}
	return true;
}
int gcd(int a,int b){
	if(b==0) return a;
	else return gcd(b,a%b);
}
int digitSum(int x) {
    int sum = 0;
    string s = to_string(x);
    for (int i = 0;i <s.length();i++)
        sum += s[i] - '0';
    return sum;
}
//
void dfs(int sum,int val,int left,int target){
	//满足情况~ 
	if(left==0&&sum==target){
		int n=digitSum(val+1),g=gcd(sum,n);
		if(g>2&&isPrime(g)) r.push_back(record(val,n));
	}else if(left>0){
		for(int i=0;i<=9;i++)
			if(sum+i+left*9-9>=target&&sum+i<=target)//剪枝
				dfs(sum+i,val*10+i,left-1,target); 
	}
}

int main(){
	int n;cin>>n;
	for(int i=1;i<=n;i++){
		int k,m;
		cin>>k>>m;
		r.clear();
		cout<<"Case "<<i<<endl;
		for(int j=1;j<=9;j++) dfs(j,j,k-1,m);//把第一位摘出来
		if(r.empty()) cout<<"No Solution\n";
		else{
			sort(r.begin(),r.end());
			for(int j=0;j<r.size();j++)
				cout<<r[j].sum<<" "<<r[j].val<<"\n";
		} 
	}
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
	return a%b==0?b:gcd(b,a%b);
}
bool prime(int x){
	if(x<=2) return false;
	for(int i=2;i<=sqrt(x);i++){
		if(x%i==0)	return false;
	}
	return true;
}
int sumd(int x){
	int sum=0;
	while(x){
		sum+=x%10;x=x/10;
	}
	return sum;
}
struct sb{
	int op,val;
}p[1000005];
bool cmp(sb u,sb w){
	if(u.op!=w.op)return u.op<w.op;
	else return u.val<w.val;
}
int main(){
	int t,n,m,k,o=1;
	scanf("%d",&t);
	while(t--){	
		int flag=0,uio=0;
		scanf("%d%d",&k,&m);
		printf("Case %d\n",o++);
		//这里总是容易写错 
		for(int i=pow(10,k-3);i<=pow(10,k-2)-1;i++){
			if(sumd(i)+18==m){
				int op=sumd(i+1);
				if(prime(gcd(m,op))){
					p[uio].op=op;
					p[uio].val=i*100+99;
					uio++;
					flag++;
				}
			}
		}
		if(flag==0)
			printf("No Solution\n");
		else{
			sort(p,p+uio,cmp);
			for(int i=0;i<uio;i++)
				printf("%d %d\n",p[i].op,p[i].val);
		}
	}
	return 0;
}

7-2

Merging Linked Lists

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

分析:

按题目说的顺序把相应的结点push进ans即可。

代码: 

#include<bits/stdc++.h>
using namespace std;
const int maxn=100100;
struct node{
    int address,data,next;
}v[maxn];
vector<node> v1,v2,ans;
int main(){
    int s1,s2,n;
    scanf("%d %d %d",&s1,&s2,&n);
    for(int i=0;i<n;i++){
        int a,b,c;
        scanf("%d %d %d",&a,&b,&c);
        v[a].address=a;v[a].data=b;v[a].next=c;
    }
    while(s1!=-1){
        v1.push_back(v[s1]);
        s1=v[s1].next;
    }
    while(s2!=-1){
        v2.push_back(v[s2]);
        s2=v[s2].next;
    }
    if(v2.size()>=2*v1.size()){
        for(int i=0,j=0;i<v2.size()||j<v1.size();i+=2,j++){
            ans.push_back(v2[i]);
	    if(i+1<v2.size()) ans.push_back(v2[i+1]);
            if(j<v1.size()) ans.push_back(v1[v1.size()-1-j]);
        }
    }else{
        for(int i=0,j=0;i<v1.size()||j<v2.size();i+=2,j++){
            ans.push_back(v1[i]);
	    if(i+1<v1.size()) ans.push_back(v1[i+1]);
            if(j<v2.size()) ans.push_back(v2[v2.size()-1-j]);
        }
    }
    for(int i=0;i<ans.size()-1;i++){
        printf("%05d %d %05d\n",ans[i].address,ans[i].data,ans[i+1].address);
    }
    printf("%05d %d -1",ans[ans.size()-1].address,ans[ans.size()-1].data,-1);
    return 0;
}

7-3

Postfix Expression

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

Sample Output 2:

(((a)(2.35)*)(-((str)(871)%))+)

分析:

就是dfs遍历树,我感觉这种题没啥,弄不出来就慢慢调 就调出来了

1、遍历的顺序是左右根,但是如果有有的没有左子树就是根右(判断一下,然后data在递归前加还是递归后加即可),这应该没什么特殊情况,毕竟是后缀表达式

2、每弄一个结点都要加括号,加括号的规律是((左)(右)根)  正常写递归就可以

代码:

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

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 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

分析:

判断正误,只需要判断下一个顶点是否在每次找到的距离最近的顶点集里。

因此将Dijkstra算法稍加改动即可。

历年真题的第四题,感觉每次都是将熟悉的做法稍加改动,不必想的太复杂。

代码: 

#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
const int maxx=10000000;
int g[maxn][maxn];
bool visit[maxn]={false};
int d[maxn];
vector<int> v; 
int n;
void check(){
    fill(d,d+maxn,maxx);
    fill(visit,visit+maxn,false);
    d[v[0]]=0;//1.
    set<int> temp;//2.
    int index=0;//3.
    for(int i=0;i<n;i++){
        int u=-1,min=maxx;
        for(int j=1;j<=n;j++){
            if(visit[j]==false){
            	if(d[j]<min){
            		min=d[j];
            		temp.clear();temp.insert(j);
				}else if(d[j]==min) temp.insert(j);
            }
        }
        //wrong
        if(temp.find(v[index])!=temp.end()){
        	visit[v[index]]=true;
        	u=v[index];
        	temp.clear();
		}else{
			cout<<"No"<<endl;return;
		} 
        for(int v=1;v<=n;v++){
            if(visit[v]==false&&g[u][v]!=maxx){
                if(d[u]+g[u][v]<d[v]){
                    d[v]=d[u]+g[u][v];
                }
            }
        }
        index++;
    }
    cout<<"Yes"<<endl;
}
int main(){
   int m;
   cin>>n>>m;
    fill(g[0],g[0]+maxn*maxn,maxx);
   for(int i=0;i<m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        g[a][b]=g[b][a]=c;
    }
    int k;cin>>k;
    for(int i=0;i<k;i++){
		v.clear();
		for(int j=0;j<n;j++){
			int a;
			cin>>a;
			v.push_back(a);
		}
		check();
	}
	return 0;
}

 

 总结:

本来觉得这一次题挺难的,但是整理过后发现确实都是熟悉的考点。

总之考试时一定要冷静~

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值