【PTA】PAT(甲级)2022年冬季考试自测

news2025/1/20 12:02:41

个人学习记录,代码难免不尽人意。

这次考试,e,第一题我看好多人都没理解题意做错了,还有第四题真的是比较意外,之前做的题都是给序列建树,但是这次让判断是否可以生成树,之前从来没有遇到过这类题,所以第四题要好好记住。第二题和第三题到是很简单。

A-1 Reduction of Proper Fractions
A Proper Fraction(真分数)is a fraction where the numerator(分子)is less than the denominator(分母). To reduce a fraction, we are supposed to cross out any common factors between the numerator and the denominator.

Here is another way to do the reduction: simply cross out any common digits. In most of the cases, this way cannot give the correct result. However, sometimes it happends to work! For example: 26/65 = 2/5 where 6 is crossed out.

Now given a proper fraction, you are supposed to tell if this simple reduction works or not.

Input Specification:
Each input file contains one test case, with the numerator a and the denominator b of the proper fraction given in a line, separated by a space. It is guaranteed that 0<a<b≤10 5.

Output Specification:
For each test case, print in a line both fractions before and after the simple reduction. If they are equal, put a = between them; else put != instead. The format is either a/b = c/d or a/b != c/d.

Sample Input 1:
39 195
Sample Output 1:
39/195 = 3/15
Sample Input 2:
1234 2345
Sample Output 2:
1234/2345 != 1/5
Sample Input 3:
1234 5678
Sample Output 3:
1234/5678 = 1234/5678

#include<iostream>
#include<cstdio>
#include<string>

using namespace std;
int table1[10]={0};
int table2[10]={0};
int temp1[10]={0};
int temp2[10]={0};
int toint(string s){
	int n=0;
	for(int i=0;i<s.size();i++){
		n=n*10+s[i]-'0';
	}
	return n;
}
int gcd(int a,int b){
	if(b==0) return a;
	else return gcd(b,a%b);
}
int main(){
	string a,b;
	cin >> a >> b;
	for(int i=0;i<a.size();i++){
			table1[a[i]-'0']++;
	}

	for(int i=0;i<b.size();i++){
		table2[b[i]-'0']++;
}
for(int i=0;i<10;i++){
	temp1[i]=table1[i];
	temp2[i]=table2[i];
}
	string a1,b1;
	for(int i=0;i<a.size();i++){
		if(table1[a[i]-'0']==0||table2[a[i]-'0']==0){
			a1=a1+a[i];
		}
		else{
			table1[a[i]-'0']--;
			table2[a[i]-'0']--;
		}
	}
	for(int i=0;i<b.size();i++){
		if(temp1[b[i]-'0']==0||temp2[b[i]-'0']==0){
			b1=b1+b[i];
		}
		else{
			temp1[b[i]-'0']--;
			temp2[b[i]-'0']--;
		}
	}
	int num1=toint(a);
	int num2=toint(b);
	int factor;
    factor=gcd(num2,num1);
	num1=num1/factor;
	num2=num2/factor;
	int temp1=toint(a1);
	int temp2=toint(b1);
	if(temp1==temp2&temp1==0){
		cout <<a<<"/"<<b<<" != 0/0";
		return 0;
	}
	int factor1;
	if(temp1>temp2)
	factor1=gcd(temp1,temp2);
	else factor1=gcd(temp2,temp1);
	temp1=temp1/factor1;
	temp2=temp2/factor1;
	if(num1==temp1&&num2==temp2){
		cout << a<<"/"<<b<<" = "<<toint(a1)<<"/"<<toint(b1);
	}
	else cout << a<<"/"<<b<<" != "<<toint(a1)<<"/"<<toint(b1);
} 

题目的意思是“对于A和B中的每一个相同的字符都去除,而不是说存在相同的都要去除”。(我感觉出题人就喜欢不把话说明白让你猜哈哈),举个例子

a=1112,b=1234,正确答案c=112,d=34
明白了这个,这道题就是送分题了。

A-2 Luggage Pickup
在这里插入图片描述
When a flight arrives, the passengers will go to the Arrivals area to pick up their baggage from a luggage conveyor belt (行李传送带).

Now assume that we have a special airport that has only one pickup window for each conveyor belt. The passengers are asked to line up to pick up their luggage one by one at the window. But if one arrives at the window yet finds out that one is not the owner of that luggage, one will have to move to the end of the queue and wait for the next turn. At the mean time, the luggage at the window will wait until its owner picks it up. Suppose that each turn takes 1 minute, your job is to calculate the total time taken to clear the conveyor belt, and the average waiting time for the passengers.

For example, assume that luggage i belongs to passenger i. If the luggage come in order 1, 2, 3, and the passengers come in order 2, 1, 3. Luggage 1 will wait for 2 minutes to be picked up by passenger 1. Then the luggage queue contains 2 and 3, and the passenger queue contains 3 and 2. So luggage 2 will wait for 2 minutes to be picked up by passenger 2. And finally 3 is done at the 5th minute. The average waiting time for the passengers is (2+4+5)/3≈3.7.

Input Specification:
Each input file contains one test case. The first line gives a positive integer N (≤10 3 ). Then N numbers are given in the next line, which is a permutation of the integers in [1,N], representing the passenger queue. Here we are assuming that the luggage queue is in order of 1, 2, …, N, and the i-th luggage belongs to passenger i.

All the numbers in a line are separated by a space.

Output Specification:
For each test case, print in a line the total time taken to clear the conveyor belt, and the average waiting time for the passengers (output 1 decimal place). The numbers must be separated by 1 space, and there must be no extra space at the beginning or the end of the line.

Sample Input:
5
3 5 1 2 4
Sample Output:
9 6.0

#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
using namespace std;
const int maxn=1010;
queue<int> luggage,people;
int minutes[maxn];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int num;
		scanf("%d",&num);
		people.push(num);
	}
	for(int i=1;i<=n;i++){
		luggage.push(i);
	}
	int cnt=0;
	
	while(!luggage.empty()&&!people.empty()){
		int now=luggage.front();
		int temp=people.front();
		if(now==temp){
			cnt++;
			minutes[now]=cnt;
			luggage.pop();
			people.pop(); 
		}
		else{
			cnt++;
			people.pop();
			people.push(temp);
		}
	}
	cout << cnt<<" ";
	int sum=0;
	for(int i=1;i<=n;i++){
		sum+=minutes[i];
	}
	double res=(1.0*sum)/(1.0*n);
	printf("%.1lf",res);
} 

这道题就是很简单的模拟题,不多说了
在这里插入图片描述In graph theory, given a connected graph G, the vertices whose removal would disconnect G are known as articulation points. For example, vertices 1, 3, 5, and 7 are the articulation points in the graph shown by the above figure (also given by the sample input).

It is a bit complicated to find the articulation points. Here you are only asked to check if a given vertex is an articulation point or not.

Input Specification:
Each input file contains one test case. For each case, the first line contains 3 positive integers: N (≤10
3
), M (≤10
4
), and K (≤100), which are the number of vertices, the number of edges, and the number of queries, respectively. Then M lines follow, each gives an undirected edge in the format:

v1 v2
where v1 and v2 are the two ends of an edge. Here we assume that all the vertices are numbered from 0 to N−1. It is guaranteed that v1 is never the same as v2, and the graph is connected.

Finally a line of queries are given, which contains K vertices to be checked. The numbers in a line are separated by spaces.

Output Specification:
Output a string of 0’s and 1’s in a line. That is, for each query, print 1 if the given vertex is an articulation point, or 0 if not. There must be no space between the answers.

Sample Input:
10 11 8
0 1
8 7
9 7
1 2
1 3
3 5
5 7
2 4
3 4
5 6
6 7
5 2 9 1 6 3 2 7
Sample Output:
10010101

#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
using namespace std;
const int maxn=1010;
const int INF=1000000000;
int G[maxn][maxn],temp[maxn][maxn];
bool visit[maxn]={false};
void dfs(int begin,int n){
	for(int i=0;i<n;i++){
		if(!visit[i]&&temp[begin][i]!=INF){
			visit[i]=true;
			dfs(i,n);
		}
	}
}
bool dfstravel(int n){

	int num=0;
	for(int i=0;i<n;i++){
		if(!visit[i]){
			visit[i]=true;
			num++;
			dfs(i,n);
		}
	}

	if(num>1) return false;
	else return true;
}
int main(){
	for(int i=0;i<maxn;i++){
		for(int j=0;j<maxn;j++){
			G[i][j]=INF;
		}
	}
      int n,m,k;
	  scanf("%d %d %d",&n,&m,&k);
	  for(int i=0;i<m;i++){
	  	int a,b;
	  	scanf("%d %d",&a,&b);
	  	G[a][b]=1;
	  	G[b][a]=1;
	  }
	for(int p=0;p<k;p++){
		int num;
		scanf("%d",&num);
		for(int i=0;i<maxn;i++){
		for(int j=0;j<maxn;j++){
			temp[i][j]=G[i][j];
		}
	}
		for(int i=0;i<n;i++){
				temp[num][i]=INF;
				temp[i][num]=INF;
		}
		fill(visit,visit+maxn,false);
		visit[num]=true;//删去节点赋为true防止再处理 
		if(dfstravel(n)){
			printf("0");
		}
		else printf("1");
	}  	
} 

这道题也很简单,不多说了。

A-4 Check Inorder Traversals
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

Now given a pair of postorder and preorder traversal sequences, you are supposed to check a set of inorder traversal sequences, and answer whether or not they are indeed inorder traversal sequences of the tree.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 30), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the postorder sequence.

Then another positive integer K (≤ 10) is given, followed by K lines of inorder traversal sequences.

Each sequence consists of N positive integers. All the numbers in a line are separated by a space.

Output Specification:
For each inorder traversal sequence, print in a line Yes if it is an inorder traversal sequence of the tree, or No if not.

On the other hand, there is another possible case that the given pre- and postorder traversal sequences can NOT form any tree at all. In that case, first print in a line Are you kidding me?. Then for each inorder traversal sequence of the input, try to construct a tree with the given preorder traversal sequence, and output in a line the correct postorder traversal sequence. If it is still impossible, output You must be kidding me! instead.

Sample Input 1:
4
1 2 3 4
2 4 3 1
4
2 1 3 4
1 2 3 4
2 1 4 3
2 1 5 6
Sample Output 1:
Yes
No
Yes
No
Sample Input 2:
7
1 2 3 4 6 7 5
6 7 5 4 3 2 1
3
2 1 6 4 7 3 5
2 1 6 7 4 3 5
2 3 1 7 4 5 6
Sample Output 2:
Are you kidding me?
2 6 7 4 5 3 1
2 7 6 4 5 3 1
You must be kidding me!

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=35;
int pre[maxn];
int post[maxn];
int in[maxn];
struct node{
	int data;
	node* lchild;
	node* rchild;
};
node* newnode(int data){
	node* root=new node;
	root->data=data;
	root->lchild=NULL;
	root->rchild=NULL;
	return root; 
}


bool flag=true;
void build(int prel,int prer,int postl,int postr){
	if(prel>=prer||postl>=postr) return;
	if(pre[prel]!=post[postr]){
		flag=false;
		return;
	}
	int index=-1;
	for(int i=postl;i<=postr;i++){
		if(post[i]==pre[prel+1]){
			index=i;
			break;
		}
	}
	if(index==-1){
		flag=false;
		return;
	}
	int leftnum=index-postl+1;
	build(prel+1,prel+leftnum,postl,index);
	build(prel+leftnum+1,prer,index+1,postr-1);
}
bool flag1=true;
node* create(int prel,int prer,int inl,int inr){
	if(prel>prer) return NULL;
	int mid=pre[prel];
	int index=-1;
	for(int i=inl;i<=inr;i++){
		if(in[i]==mid){
			index=i;
			break;
		}
	}
	if(index==-1){
		flag1=false;
		return NULL;
	}
	int leftnum=index-inl;
	node* root=newnode(mid);
	root->lchild=create(prel+1,prel+leftnum,inl,index-1);
	root->rchild=create(prel+1+leftnum,prer,index+1,inr);
	return root;
}
int temp[maxn];
int cnt=0;
void dfs(node* root){
	if(root==NULL) return ;
	dfs(root->lchild);
	dfs(root->rchild);
	temp[cnt++]=root->data;
}
int main(){
     int n;
	 scanf("%d",&n);
	 for(int i=0;i<n;i++){
	 	scanf("%d",&pre[i]);
	 }	
	 for(int i=0;i<n;i++){
	 	scanf("%d",&post[i]);
	 }
	 build(0,n-1,0,n-1);
	 
	 if(flag){
        int k;
        scanf("%d",&k);
        for(int i=0;i<k;i++){
        	for(int j=0;j<n;j++) scanf("%d",&in[j]);
        	flag1=true;
        	node* root=create(0,n-1,0,n-1);
        	if(!flag1){
        		printf("No\n");
			}
			else{
				cnt=0;
				dfs(root);
				int m=0;
				for(;m<n;m++){
					if(post[m]!=temp[m]){
						break;
					}
				}
				if(m!=n){
					printf("No\n");
				}
				else printf("Yes\n");
			}
		}
		
	 }
	 else{
	 	int k;
        scanf("%d",&k);
		printf("Are you kidding me?\n");
        for(int i=0;i<k;i++){
            for(int j=0;j<n;j++) scanf("%d",&in[j]);
            flag1=true;
		    node* root=create(0,n-1,0,n-1);
		    if(!flag1){
		    	printf("You must be kidding me!\n");
			}
			else{
				cnt=0;
				dfs(root);
				for(int j=0;j<n;j++){
					printf("%d",temp[j]);
					if(j!=n-1)printf(" ");
					else printf("\n");
				}
			}
		}
	 }
}

这道题需要熟练掌握!一开始我想的有点复杂了,其实判断前序序列和后序序列能不能生成一个二叉树只需要判断pre的第一个和post的最后一个是否相等即可!除此之外,题目还有一个坑点是两个序列不一定是完全覆盖的,需要判断!

各位肯定对用前序序列和中序序列建树的过程和方法烂熟于心了!那么我们就可以用模板来举一反三求解此题。我们可以用同样的方法来处理左右子树,如果各自序列对应的根节点不相等就返回false,除此之外,因题目的特殊要求,如果在第二个序列查找不到对应的数字,就要返回false。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1007508.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

leecode 每日一题 2596. 检查骑士巡视方案

2596. 检查骑士巡视方案 骑士在一张 n x n 的棋盘上巡视。在 有效 的巡视方案中&#xff0c;骑士会从棋盘的 左上角 出发&#xff0c;并且访问棋盘上的每个格子 恰好一次 。 给你一个 n x n 的整数矩阵 grid &#xff0c;由范围 [0, n * n - 1] 内的不同整数组成&#xff0c;其…

pyechart练习(一):画图小练习

1、使用Map制作全球人口分布图 import math import osimport matplotlib.pyplot as plt from pyecharts.charts import Map from pyecharts import options as opts# 只有部分国家的人口数据 POPULATION [["China", 1420062022], ["India", 1368737513],…

【C++】拷贝构造函数调用时机 ② ( 对象值作为函数参数 | 对象值作为函数返回值 )

文章目录 一、拷贝构造函数概念二、对象值作为函数参数1、拷贝构造函数调用情况说明2、代码示例 - 对象值作为函数参数 三、对象值作为函数返回值1、拷贝构造函数调用情况说明2、代码示例 - 对象值作为函数返回值 博客总结 : " 拷贝构造函数 " 又称为 " 赋值构…

MySQL之分布式事务

写在前面 当数据库进行了分库分表 之后为了保证数据的一致性。不可变的就需要引入跨数据的事务解决方案&#xff0c;这种解决方案我们叫做分布式事务。本文就一起来看下分布式事务相关的内容。 在8.0 版本上学习。 1&#xff1a;实战 为了能够更好的理解理论知识&#xff0c;…

基于SSM的中小型企业财务管理系统的设计与实现

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;采用JSP技术开发 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#x…

C++项目实战——基于多设计模式下的同步异步日志系统-②-前置知识补充-不定参函数

文章目录 专栏导读不定参函数C风格不定参函数不定参宏函数 专栏导读 &#x1f338;作者简介&#xff1a;花想云 &#xff0c;在读本科生一枚&#xff0c;C/C领域新星创作者&#xff0c;新星计划导师&#xff0c;阿里云专家博主&#xff0c;CSDN内容合伙人…致力于 C/C、Linux 学…

数据分析三剑客之Matplotlib

0.Matplotlib绘图和可视化 1.简介 我的前面两篇文章介绍了 Nimpy &#xff0c;Pandas 。今天来介绍一下Matplotlib。 简单来说&#xff0c;Matplotlib 是 Python 的一个绘图库。它包含了大量的工具&#xff0c;你可以使用这些工具创建各种图形&#xff0c;包括简单的散点图&…

Java多线程并发面试题

文章目录 Java并发基础并行和并发有什么区别&#xff1f;说说什么是进程和线程&#xff1f;Java线程创建方式&#xff1f;Runnable和Callable接口的区别&#xff1f;为什么调用start()方法时会执行run()方法&#xff0c;不直接调用run()方法&#xff1f;sleep()和wait()的区别&…

预约陪诊就诊小程序源码多城市开发版

陪诊小程序多城市版开发 小程序支持多城市开通&#xff0c;支持创建陪诊团队以及提成奖励设置&#xff0c;可以定义多种服务类型&#xff0c;订单流程简单明了&#xff0c;支持陪诊师手机端订单处理&#xff0c;家政类目可以轻松过审。 小程序市场前景&#xff1a; 人口老龄化…

强大:dynamsoft-barcode-reader-dotnet-9.6.30 Crack

dynamsoft-barcode-reader-dotnet 具有灵活 API 的强大条码扫描器 SDK 无论它是扭曲的、黑暗的、遥远的、模糊的、批量的还是移动的&#xff0c;我们都可以扫描它。速度快 条码扫描速度每分钟500 业界最快的扫描解码&#xff0c;可应用于不同场景&#xff1a; 多个条形码/二维…

【VS2022】调试

F9 创建或取消断点 ctrlF9 禁用断点 F5 开始调试&#xff08;到断点处停下来&#xff09; F10 逐过程&#xff08;不进入函数&#xff09; F11 逐语句 F5、F10、F11都可以直接进入调试 【调试】->【窗口】->【监视】&#xff0c;输入变量就可以观察到变量的值。 …

C语言数组和指针笔试题(二)(一定要看)

目录 字符数组二例题1例题2例题3例题4例题5例题6例题7总结 字符数组三例题1例题2例题3例题4例题5例题6例题7 字符数组二 char arr[] {a,b,c,d,e,f} 1:printf("%d\n", strlen(arr)); 2:printf("%d\n", strlen(arr0)); 3:printf("%d\n", strlen(…

Python小项目之Tkinter应用】随机点名/抽奖工具大优化:新增查看历史记录窗口!语音播报功能!修复预览文件按钮等之前版本的bug!

文章目录 前言一、实现思路二、关键代码查看历史记录按钮语音播报按钮三、完整代码总结前言 老生常谈,先看效果:(订阅专栏可获取完整代码) 初始状态下,我们为除了【设置】外的按钮添加弹窗,提示用户在使用工具之前要先【设置】。在设置界面,我们主要修改了【预览文件】…

每日一题 2596. 检查骑士巡视方案

难度&#xff1a;中等 很简单&#xff0c;从第 0 步开始模拟即可&#xff0c;唯一sb的就是测试用例中如果&#xff08;0&#xff0c;0&#xff09;处不为0的话就直接false&#xff0c;而不是去找0在哪 我的代码&#xff1a; class Solution:def checkValidGrid(self, grid: L…

2000-2021年上市公司全要素生产率数据OP法(含原始数据+计算代码+结果)

2000-2021年上市公司全要素生产率数据OP法&#xff08;含原始数据计算代码结果&#xff09; 1、时间&#xff1a;2000-2021年 2、指标&#xff1a;股票代码、年份、证券代码、固定资产净额、营业总收入、营业收入、营业成本、销售费用、管理费用、财务费用、支付给职工以及为…

防火墙 (五十四)

目录 前言 一、防火墙作用 二、防火墙分类 三、防火墙性能 四、硬件防火墙 五、软件防火墙 5.1 iptables 六、iptables应用 前言 本文就简单的介绍了防火墙的基础内容和一些简单案例的操作。 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考 一、防火墙…

jmeter线程组 bzm - Concurrency Thread Group 阶梯式压测

简介 bzm - Concurrency Thread Group 不是JMeter的官方插件&#xff0c;而是一种由Blazemeter提供的高级线程组插件&#xff0c;它提供了更灵活的并发性能测试设置。它可以在不同的时间内并发执行不同数量的线程&#xff0c;模拟不同的负载场景。 插件下载地址&#xff08;jme…

信创办公–基于WPS的EXCEL最佳实践系列 (通过函数实现灵活限制)

信创办公–基于WPS的EXCEL最佳实践系列 &#xff08;通过函数实现灵活限制&#xff09; 目录 应用背景操作步骤1、sum函数的应用2、and函数的应用3、日期相关函数的应用4、eomonth函数的应用 应用背景 在Excel表格中&#xff0c;我们经常遇到需要大量计算或需要特殊处理的数据…

安达发|家具制造业APS高级计划与排程解决方案

在当今竞争激烈的市场环境下&#xff0c;家具制造业面临着诸多挑战&#xff0c;如原材料价格波动、人工成本上升、市场需求不确定性等。为了降低成本、提高生产效率和满足客户需求&#xff0c;家具制造企业需要寻求有效的计划与排程解决方案。本文将介绍一种基于先进生产计划系…

基于SSM的彩妆小样售卖商城

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;采用JSP技术开发 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#x…