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

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

在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.hqwc.cn/news/102924.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[MySQL]查看数据库大小

查看库大小 例如&#xff1a;查看当前MySQL中数据总量超过2GB的库&#xff1a; select table_schema as 数据库,table_rows as 记录数,data_size as 数据容量(GB),index_size as 索引容量(MB) from (selecttable_schema,sum(table_rows) as table_rows,sum(truncate(data_leng…

C++毕业设计基于QT实现的超市收银管理系统源代码+数据库

C毕业设计基于QT实现的超市收银管理系统源代码数据库 编译使用 编译完成后&#xff0c;需要拷贝 file目录下的数据库 POP.db文件到可执行程序目录下 登录界面 主界面 会员管理 完整代码下载地址&#xff1a;基于QT实现的超市收银管理系统源代码数据库

高频golang面试题:简单聊聊内存逃逸?

文章目录 问题怎么答举例 问题 知道golang的内存逃逸吗&#xff1f;什么情况下会发生内存逃逸&#xff1f; 怎么答 golang程序变量会携带有一组校验数据&#xff0c;用来证明它的整个生命周期是否在运行时完全可知。如果变量通过了这些校验&#xff0c;它就可以在栈上分配。…

Vmware 网络恢复断网和连接

如果你的 虚拟机无法联网了&#xff0c;比如&#xff1a; vmware 无法将网络更改为桥接状态: 没有未桥接的主机网络适配器 等各种稀奇古怪的问题&#xff1b; 按照下面操作 还远默认设置 包你解决各种问题&#xff01;

自然语言处理学习笔记(八)———— 准确率

目录 1.准确率定义 2.混淆矩阵与TP/FN/FP/TN 3. 精确率 4.召回率 5.F1值 6.中文分词的P、R、F1计算 7.实现 1.准确率定义 准确率是用来衡量一个系统的准确程度的值&#xff0c;可以理解为一系列评测指标。当预测与答案的数量相等时&#xff0c;准确率指的是系统做出正确判…

ECS通过DNAT将C非专线网段并网

1.问题描述 客户需求&#xff1a;ECS1需要访问140.131.208.0/24 &#xff0c;由于140.131.208.0/24网段属于公网地址&#xff0c;在CSW侧为进行并网。 解决方案&#xff1a;故将ECS1发起的请求其在云内ECS2做DNAT&#xff0c;将该网段转换成CSW并网网段170.101.253.0/24&…

【JavaScript】JS语法入门到实战

文章目录 一、初识JavaScript1. 什么是JavaScript&#xff1f;2. JavaScript 和 HTML 和 CSS 之间的关系3. JavaScript的运行过程4. JavaScript的组成 二、JavaScript的书写形式三、变量1. 输入输出2. 变量的使用3. 数据类型 四、运算符五、分支和循环语句1. 分支语句2. 循环语…

20230831-完成登录框的按钮操作,并在登录成功后进行界面跳转

登录框的按钮操作&#xff0c;并在登录成功后进行界面跳转 app.cpp #include "app.h" #include <cstdio> #include <QDebug> #include <QLineEdit> #include <QLabel> #include <QPainter> #include <QString> #include <Q…

Linux:进程(概念)

学习目标 1.认识冯诺依曼系统 2.认识操作系统概念与定位 (系统调用接口) 3.理解进程的概念&#xff08;PCB&#xff09; 4.理解进程的状态&#xff08;fork创建进程&#xff0c;僵尸进程及孤儿进程&#xff09; 5.了解进程的调度&#xff08;优先级&#xff0c;竞争性&#xff…

LeetCode(力扣)46. 全排列Python

LeetCode46. 全排列 题目链接代码 题目链接 https://leetcode.cn/problems/permutations/ 代码 class Solution:def backtracking(self, nums, result, path, used):if len(path) len(nums):result.append(path[:])for i in range(len(nums)):if used[i]:continuepath.app…

53、springboot对websocket的支持有两种方式-------1、基于注解开发 WebSocket ,简洁实现多人聊天界面

基于注解开发 WebSocket –注解就是&#xff1a; OnOpen、 OnClose 、 OnMessage 、OnError这些 ★ WebSocket的两种开发方式 ▲ Spring Boot为WebSocket提供了两种开发方式&#xff1a; 基于spring-boot-starter-websocket.jar开发WebSocket 基于Spring WebFlux开发WebSoc…

C++(QT)画图行车

通过鼠标在窗口上点击形成多个点的连线&#xff0c;绘制一辆汽车沿着绘制的连线轨迹前进。要求连线点数大于20.可以通过清除按钮清除已经绘制的连线&#xff0c;并可以重新绘制一条轨迹连线。当车辆行驶到轨迹终点时&#xff0c;自动停止。&#xff08;汽车实在可用方块代替&am…