【PTA】浙软2020年上机题目自测

news2024/10/5 14:38:53

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

在PTA买了浙软2020年的保研上机真题+时光机做了做,20年的明显要比19年的难一些,我用了差不多2小时多一点做完了,最后得分90分,在当年排名26左右。下面是4道题和我的做法

7-1 Standard Form of Polynomial
在这里插入图片描述
Sample Input:
3
-1 4 -1
Sample Output:
-2 -7 -4

#include<iostream> 
#include<cstdio>
#include<vector>
int num[15];
using namespace std;
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
	}
	vector<int> res,temp;
	res.push_back(1);
	res.push_back(-num[0]);
	for(int i=1;i<n;i++){
		temp.clear();//别忘了;
		temp.push_back(1);
		temp.push_back(-num[i]);//注意加负 
	
		vector<int> v1,v2;
		for(int j=0;j<res.size();j++){
			v1.push_back(res[j]*temp[0]);
			v2.push_back(res[j]*temp[1]);
		}
		res.clear();
		res.push_back(v1[0]);
		for(int j=1;j<v1.size();j++){
			res.push_back(v1[j]+v2[j-1]);
		}
		res.push_back(v2[v2.size()-1]);

	}
	for(int i=1;i<res.size();i++){
		printf("%d",res[i]);
		if(i!=res.size()-1) printf(" ");
		else printf("\n");
	}
}

第一题难度适中,如果一开始没有思路的话也不要着急,时间是很充裕的。题干的意思是给了化简的多项式(x-n1)(x-n2)(x-n3)·······,我们需要将他们转换成一般形式再输出。我们可以这样分析,用temp保存每次计算的系数项,然后和下一个括号相乘,然后再错位相加即可。

7-2 Distance of Triples
在这里插入图片描述Output Specification:
For each case, print in a line MinD(a, b, c) = d, where (a, b, c) are the triples that has the minimum distance, and d is the corresponding distance. If the solution is not unique, output the largest triples.

Sample Input:
4 4 6
0 9 -1 11
10 -25 11 -10
9 2 41 17 12 30
Sample Output:
MinD(11, 11, 12) = 2
Hint:
Notice that there are two solutions. The other one is MinD(9, 10, 9) = 2. Since (11, 11, 12) is larger, this one must be printed out.

#include<iostream> 
#include<cstdio>
#include<vector>
#include<unordered_set>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
const int maxn=30010;
vector<pair<int,char> > v;
bool cmp(pair<int,char> a,pair<int,char> b){
	if(a.first!=b.first)
	return a.first<b.first;
	else return a.second<b.second;
}
int main(){
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    for(int i=0;i<a;i++){
    	int num;
    	scanf("%d",&num);
    	v.push_back(make_pair(num,'a'));
	}
	for(int i=0;i<b;i++){
    		int num;
    	scanf("%d",&num);
    	v.push_back(make_pair(num,'b'));
	}
	for(int i=0;i<c;i++){
    		int num;
    	scanf("%d",&num);
    	v.push_back(make_pair(num,'c'));
	}
	sort(v.begin(),v.end(),cmp);
	int min=1000000000;
	int arr[3]={0};
	for(int i=0;i<v.size()-2;i++){
		char tag1=v[i].second;
		char tag2=v[i+1].second;
		char tag3=v[i+2].second;
		if(tag1!=tag2&&tag2!=tag3&&tag3!=tag2){
			int num1=v[i].first;
			int num2=v[i+1].first;
			int num3=v[i+2].first;
			int res=abs(num1-num2)+abs(num2-num3)+abs(num3-num1);
			if(res<=min){
				min=res;
				arr[tag1-'a']=num1;
				arr[tag2-'a']=num2;
				arr[tag3-'a']=num3;
			}
			
		}
	}
	printf("MinD(%d, %d, %d) = %d\n",arr[0],arr[1],arr[2],min);
}

这道题我得了19分,说实话确实有点不大会做,我只想到了将a,b,c放到一个数组中从小到大排列,然后觉得满足题意的a,b,c应该是挨在一起的,但是这样子并不行。

7-3 Partial School Ranking
In a Group Programming Contest, each university is supposed to send n students who must compete independently. The universities are ranked according to the total score of their students. Your job is to generate the ranklist.

It sounds like a simple problem, but what makes it complicated is that we have lost all the teams’ information! What we have is a list of only some of the students’ scores, and some photos taken from the contest sites which shows several students with the same university badge. You just have to recover the ranklist as much as you can.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤1000). Then N lines follow, each gives the information of a student in the format:

ID k teammate
1

⋯teammate
k

Score

where ID is a unique 4-digit identification number for a student; k (0≤k≤5) is the number of teammates of this student in a photo; teammate
i

's are the ID’s of his/her teammate; and Score is this stdent’s score which is in the range [0, 400].

It is guaranteed that each ID with a Score is given only once.

Output Specification:
For each case, first print in a line the number of universities (all the students that are related directly or indirectly as teammates are considered in the same university). Then output the partial school ranking in the format:

ID S Score
total

where ID is the smallest student ID in the university; S is the total number of its students; and Score
total

is the total score that can be recovered for that university. The universities must be given in descending order of their Score
total

, or in ascending order of S if there is a tie, or in ascending order of ID if there is still a tie.

Sample Input:
11
7456 3 7457 7458 7459 157
6666 3 5551 5552 7777 100
1234 3 5678 9012 0002 80
8888 0 340
2468 3 0001 0004 2222 110
7777 1 6666 57
3721 1 2333 30
9012 3 1236 1235 1234 10
1235 2 5678 9012 50
2222 4 1236 2468 6661 6662 16
2333 4 3721 6661 6662 6663 44
Sample Output:
4
8888 1 340
0001 15 340
5551 4 157
7456 4 157

#include<iostream> 
#include<cstdio>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=10010;
int father[maxn];
int score[maxn]={0};
int findfather(int n){
	if(father[n]==n) return n;
	else return findfather(father[n]);
}
void merge(int a,int b){
	int fa=findfather(a);
	int fb=findfather(b);
	if(fa!=fb){
		father[fa]=fb;
	}
}
set<int> s;
struct node{
	int fa;
	int id;
	int num;
	int score;
}; 
vector<node> res;
set<int> ids;
bool cmp(node a,node b){
	if(a.score!=b.score) return a.score>b.score;
	else if(a.num!=b.num) return a.num<b.num;
	else return a.id<b.id;
}
int main(){
       for(int i=0;i<maxn;i++) father[i]=i;
       int n;
       scanf("%d",&n);
       for(int i=0;i<n;i++){
       	int id,k;
       	scanf("%d %d",&id,&k);
       	s.insert(id);
       	for(int j=0;j<k;j++){
       		int a;
       		scanf("%d",&a);
       		s.insert(a);
       		merge(a,id);
		   }
		int b;
		scanf("%d",&b);
		score[id]=b;
	   }
	   for(set<int>::iterator it=s.begin();it!=s.end();it++){
	   	       int id=*it;
	   	       int fa=findfather(id);
	   	       if(ids.find(fa)==ids.end()){
	   	       	ids.insert(fa);
	   	       	node no;
	   	       	no.fa=fa;
	   	       	no.id=id;
	   	       	no.num=1;
	   	       	no.score=score[id];
	   	       	res.push_back(no);
				}
				else{
					for(int i=0;i<res.size();i++){
						if(res[i].fa==fa){
							if(res[i].id>id) res[i].id=id;
							res[i].num++;
							res[i].score+=score[id];
						}
					}
				}
	   }
	sort(res.begin(),res.end(),cmp);
	printf("%d\n",res.size());
	for(int i=0;i<res.size();i++){
		printf("%04d %d %d",res[i].id,res[i].num,res[i].score);
		if(i!=res.size()-1) printf("\n");
	}
}

一开始我觉得我的做法会超时,结果满分通过了。本题就是很常规的存储加排序。

7-4 Shopping With Coupons
在这里插入图片描述Sample Input:
4 30
12 20 15 10
9 6 8 7
Sample Output:
8 2

#include<iostream> 
#include<cstdio>
#include<vector>
#include<unordered_set>
#include<algorithm>
#include<map>
using namespace std;
const int maxn=100010;
int cost[maxn];
int coupon[maxn];
int mp[maxn];
int main(){
      int n,money;
      scanf("%d %d",&n,&money);
      for(int i=0;i<n;i++){
      	scanf("%d",&cost[i]);
	  }
	  for(int i=0;i<n;i++){
      	scanf("%d",&coupon[i]);
	  }
	  for(int i=0;i<n;i++){
	  	for(int j=0;j<n;j++){
	  		int a=cost[i]-coupon[j];
	  		if(a<=money){
	  				mp[a]++;
				  
			
			  }
	  		
		  }
	  }
	  int cnt=0;
	  bool flag=false;
	  for(int i=0;i<maxn;i++){
	  	if(mp[i]!=0){
	  		int pay=i;
		  int len=mp[pay];
	  	  for(int i=0;i<len;i++){
	  	  	if(money>=pay){
	  	  		money-=pay;
	  	  		cnt++;
				}
			else{//及时剪枝 
				flag=true;
				break;
				
			}
			}
		  }
	  	if(flag) break;
	  }
	  printf("%d %d\n",cnt,money);
}

我的做法26/30,有一个测试点显示超时了,但是我想不出在代码基础上可以优化的地方了,感觉如果想不超时只能换一个思路了,这个地方就不细究了。

总的来说题目有难度,但是如果静下心来,不受时间的和结果的压力的话,是可以取得比较乐观的分数的。

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

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

相关文章

YOLO-NAS | YOLO新高度,引入NAS,出于YOLOv8而优于YOLOv8

编辑 | Happy 首发 | AIWalker 链接 | https://mp.weixin.qq.com/s/FsWSRguAn2WZKtmPhMbc6g 亮点在哪里&#xff1f; 参考QARepVGG&#xff0c;该方案引入了QSP与QCI模块以同时利用重参数与8-bit量化的优化&#xff1b; 该方案采用 AutoNAC搜索最优尺寸、每个stage的结构&#…

CK_Label-V23货架标签(电池版本)接口文档

查询标签信息接口 接口类型&#xff1a;POST, 参数格式&#xff1a;json array 链接形式&#xff1a; http://localhost/wms/associate/getTagsMsg 代码形式&#xff1a; { url : http://localhost/wms/associate/getTagsMsg, requestMethed : GET, requestParameter :…

第二证券:光刻胶概念再度活跃,蓝英装备近3日大涨56%

光刻胶概念8日再度走强&#xff0c;截至发稿&#xff0c;蓝英配备“20cm”涨停&#xff0c;腾景科技、&#xff0c;容大感光涨约15%&#xff0c;国风新材、奥普光电、张江高科、百川股份等涨停&#xff0c;炬光科技涨超9%。 蓝英配备近3日累计大涨56%&#xff0c;其近来在互动…

代码随想录 - Day35 - 回溯:重新安排行程,棋盘问题

代码随想录 - Day35 - 回溯&#xff1a;重新安排行程&#xff0c;棋盘问题 332. 重新安排行程 输入&#xff1a;tickets [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],[&qu…

为什么客户跟踪对客户成功很重要?如何正确做到这一点?

如果您想以客户为中心&#xff0c;了解您的客户就非常重要。您可以利用客户沟通管理平台&#xff0c;例如SaleSmartly&#xff08;SS客服&#xff09;查看各种指标来了解客户对您的实际体验以及他们对您的期望。您需要长时间跟踪它们&#xff0c;注意它们的变化并找出原因&…

JavaEE初阶(1)(冯诺依曼体系、CPU、CPU基本原理、如何衡量CPU的好坏?指令、操作系统、操作系统“内核”)

目录 冯诺依曼体系&#xff08;Von Neumann Architecture&#xff09; CPU CPU基本原理&#xff1a; 如何衡量CPU的好坏&#xff1f; 1、主频&#xff08;时钟速度&#xff09;&#xff1a; 2、核心数&#xff1a; 指令 操作系统 操作系统“内核” 冯诺依曼体系&#x…

微信小程序 房贷计算器 js代码终极版

这里写目录标题 展示图1.在utils 中创建文件calculateMortgage.ts2. 在需要使用的地方引入并传参 展示图 1.在utils 中创建文件calculateMortgage.ts /** 假设房贷本金是60万&#xff0c;贷款年利率为4.8%即月利率为0.4%&#xff0c;分20年即240期偿还&#xff0c;等额本金还款…

OpenCV(三十二):轮廓检测

1.轮廓概念介绍 在计算机视觉和图像处理领域中&#xff0c;轮廓是指在图像中表示对象边界的连续曲线。它是由一系列相邻的点构成的&#xff0c;这些点在边界上连接起来形成一个封闭的路径。 轮廓层级&#xff1a; 轮廓层级&#xff08;Contour Hierarchy&#xff09;是指在包含…

哭了,python自动化办公,终于支持 Mac下载了

想了解更多精彩内容&#xff0c;快来关注程序员晚枫 大家好&#xff0c;这里是程序员晚枫&#xff0c;小红薯/小破站也叫这个名。 在我的主页发布的免费课程&#xff1a;给小白的《50讲Python自动化办公》&#xff0c;一直在更新中&#xff0c;昨晚12点多&#xff0c;有朋友在…

1987-2021年全国31省专利申请数和授权数

1987-2021年全国31省国内三种专利申请数和授权数 1、时间&#xff1a;1987-2021年 2、来源&#xff1a;整理自国家统计局、科技统计年鉴、各省年鉴 3、范围&#xff1a;31省市 4、指标&#xff1a;国内专利申请受理量、国内发明专利申请受理量、国内实用新型专利申请受理量…

PCIe 5.0验证实战,经常遇到的那些问题?

PCIe 5.0是当前最新的PCI Express规范&#xff0c;提供了更高的数据传输速率和更大的带宽。 PCIe是连接两个芯片的接口&#xff0c;负责两个芯片通信, 连接芯片的通路为高速SerDes, 称之为链路。PCIe确保通路正常-链路训练状态机。PCIe在芯片内部是非常重要的一个大的模块&…

php常用算法

许多人都说 算法是程序的核心&#xff0c;一个程序的好于差,关键是这个程序算法的优劣。作为一个初级phper&#xff0c;虽然很少接触到算法方面的东西 。但是对于冒泡排序&#xff0c;插入排序&#xff0c;选择排序&#xff0c;快速排序四种基本算法&#xff0c;我想还是要掌握…

论机器生产内容 MGC 与新数字时代的两个世界

摘要&#xff1a;本文从新数字时代人类社会的两种存在形态&#xff1a;数字世界&#xff08;元宇宙&#xff09;与物理世界&#xff08;时空宇宙&#xff09;&#xff0c;以及新兴数字产业&#xff1a;机器生产内容MGC的发展、现状与未来出发&#xff0c;通过对新数字时代及两个…

Mysql 入门篇之二进制安装

文章目录 Mysql 5.7 入门安装卸载自带组件下载二进制包安装配置 Mysql 8.0 入门安装卸载自带组件下载二进制包安装配置 Mysql 5.7 入门安装 环境说明&#xff1a;CentOS Linux release 7.6.1810 (Core) 4核4G 卸载自带组件 卸载自带的mysql相关组件 rpm -qa | grep mysql rpm…

jemalloc 5.3.0源码总结

注意&#xff1a;jemalloc 的最新版本里没有所谓的 huge class&#xff0c;bin 中slab外面也不再套一个run的概念了&#xff0c;看其它人分享的文章时需要注意。 简述 用户侧通过 tcache 来访问&#xff0c;tcache 是一个线程的申请又释放的对象的缓存&#xff0c;它绑定了一…

紫光电子档案管理系统存在SQL注入漏洞(漏洞复现)

文章目录 紫光电子档案管理系统存在SQL注入漏洞&#xff08;漏洞复现&#xff09;0x01 前言0x02 漏洞描述0x03 影响范围0x04 漏洞环境0x05 漏洞复现1.访问漏洞环境2.构造POC3.复现 紫光电子档案管理系统存在SQL注入漏洞&#xff08;漏洞复现&#xff09; 0x01 前言 本次测试仅…

数据结构与算法-----指针与结构体

目录 前言 指针 定义 示例1&#xff08;访问修改数据&#xff09;&#xff1a; 示例2&#xff08;野指针&#xff09;&#xff1a; 示例3&#xff08;动态空间分配问题&#xff09;&#xff1a; 示例4&#xff08;字符串赋值错误问题&#xff09;&#xff1a; 示例5&am…

2023年限售股解禁研究报告

第一章 概述 解禁是指限售流通股过了限售承诺期&#xff0c;可以在二级市场自由买卖的过程。根据流通性质&#xff0c;可将上市公司股份分为有限售条件的流通股&#xff08;“限售流通股”&#xff09;及无限售条件的流通股&#xff08;“流通股”&#xff09;。 限售流通股指…

案例实战-Spring boot Web

准备工作 需求&环境搭建 需求&#xff1a; 部门管理&#xff1a; 查询部门列表 删除部门 新增部门 修改部门 员工管理 查询员工列表&#xff08;分页、条件&#xff09; 删除员工 新增员工 修改员工 环境搭建 准备数据库表&#xff08;dept、emp&#xff09; -- 部门管理…

NLP(3)--GAN

目录 一、概述 二、算法过程 三、WGAN 1、GAN的不足 2、JS散度、KL散度、Wasserstein距离 3、WGAN设计 四、Mode Collapse and Mode Dropping 1、Mode Collapse 2、Mode Dropping 3、FID 四、Conditional GAN 一、概述 GAN&#xff08;Generative Adversial Networ…