2023夏-PAT甲级题解

news2024/11/18 9:49:02

目录

总结:

A-1 Trap

Input Specification:

Output Specification:

Sample Input:

Sample Output:

题意:

思路:

AC代码:

A-2 Queue Using Two Stacks

Input Specification:

Output Specification:

Sample Input:

Sample Output:

题意:

AC代码:

A-3 Rank of Binary Tree

Input Specification:

Output Specification:

Sample Input:

Sample Output:

题意:

思路:

A-4 Big Number

Input Specification:

Output Specification:

Sample Input:

Sample Output:

题意:

思路:

 (26分)

正解:

AC代码


总结:

        第一次打PAT甲级可能也是最后一次打了,可能因为今天蓝桥国赛,就我一个人考,考场上主要还是不太适应英文题面,老是看样例猜题意,第一个题一看是个地图乍以为是个搜索,读了20分钟题看懂题意是个模拟,交了一发13分,想不出来测试点,于是看第二题,看样例以为直接用队列就行了,沉下心看题目,结果要按题意双栈模拟队列,调试了20分钟AC了,第三题常规的建树,求每个节点的度,调试20分钟AC了,这时一看排名33。就接着看第四题,看样例和题意都没看懂,这时还有1个半小时结束,后面又排序骗了5分,最后又回去想第一题的测试点,测不出来。一个半小时坐牢,就这样结束了这次考试。

        赛后又补了下题,找队友一起读题读懂了A4, 还没测,以下代码是都是凭记忆写的,等过两天可以测题了,会再更新。

        2023.6.11:已补完A1, A4题目,以下为AC代码。

A-1 Trap

A robot is designed to move on a map from South toward North. When some obstacle is encountered, the robot can only turn toward West and moves until it can turn toward North and continue.

Given a squared map with n×n blocks, a robot can start from any position below the bottom line (South) and its target is to reach the top line (North). (By the way, kindly remind you that the left-hand-side of the map is West and the right-hand-side is East.)

If some obstacles are placed in the map, the robot might get trapped if it starts from certain positions and can never reach the North line. For example, in the situation given by the following figure, the robot will be trapped if it starts from either position 7 or 8.

Your job is to point out those starting positions which will get the robot trapped.

Note: it is assumed that a robot can move out of the map boundary, and all the blocks around the map are open, without any obstacle. Hence if the robot starts from any position out of the West or East boundary, it can certainly reach North without any problem. Therefore we only have to consider the positions between the West and East boundaries (e.g. the positions from 1 to 10 below the South line in the above figure). Besides, as long as the robot can reach the North line, it is considered successful even of it ends up at out of the boundary (e.g. the robot will have to move out of the map if starts from either the positions 1 or 2, but it can still reach the North line).

Input Specification:

Each input file contains one test case. Each case starts from a positive integer n (≤100) in a line, which is the size of the map. Then n lines follow, each contains n characters, which are either 0 for an open block, or 1 for a block with obstacle. The first line corresponds to the North boundary and the last line the South.

Output Specification:

Output in a line all the starting positions which can get the robot trapped, in increasing order. The positions are indexed from West to East, starting from 1. It is guaranteed that there is at least one output.
All 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:

10
0000000000
0000111010
1100100011
0000110001
0000000011
0000000000
0100000100
0001000000
0001000000
0001100000

Sample Output:

7 8

题意:

给定一个N*N的地图,地图上有障碍物,规定从最后一行走,即最后一行列为1~n的为起点,可以先一直往上走,遇到障碍物后往左试探走一步再往上走,若再遇到障碍物,反复这样做。求不能走出地图的起点。

思路:

模拟,枚举每个起点即可

AC代码:

#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 1e2 + 7;
 
int n;
vector<int> ans;
int g[N][N];
 
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            scanf("%1d", &g[i][j]);
        }
    }
    for (int j = 1; j <= n; j++) {
        // int x = n + 1, y = j;
        // while (g[x][j] == 0 && x >= 1) x --;
        // while (g[x + 1][y] == 0 && y >= 1) y --;
        // if (y != 0) ans.push_back(j);
        int x = n + 1, int y = j;
        while (x != 0) {
            if (g[x - 1][y] == 0) {
                x--;
            } else if (g[x][y - 1] == 0) {
                y--;
            } else {
                ans.push_back(j);
                break;
            }
        }
    }
    for (int i = 0; i < (int)ans.size(); i++) {
        if (i) printf(" ");
        printf("%d", ans[i]);
    }
    return 0;
}

A-2 Queue Using Two Stacks

A queue (FIFO structure) can be implemented by two stacks (LIFO structure) in the following way:

  1. Start from two empty stacks s1​ and s2​.
  2. When element e is enqueued, it is actually pushed onto s1​.
  3. When we are supposed to dequeue, s2​ is checked first. If s2​ is empty, everything in s1​ will be transferred to s2​ by popping from s1​ and immediately pushing onto s2​. Then we just pop from s2​ -- the top element of s2​ must be the first one to enter s1​ thus is the first element that was enqueued.

Assume that each operation of push or pop takes 1 unit of time. You job is to tell the time taken for each dequeue.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤103), which are the number of operations. Then N lines follow, each gives an operation in the format

Operation Element

where Operation being I represents enqueue and O represents dequeue. For each IElement is a positive integer that is no more than 106. No Element is given for O operations.
It is guaranteed that there is at least one O operation.

Output Specification:

For each dequeue operation, print in a line the dequeued element and the unites of time taken to do this dequeue. The numbers in a line must be separated by 1 space, and there must be no extra space at the beginning or the end of the line.
In case that the queue is empty when dequeue is called, output in a line ERROR instead.

Sample Input:

10
I 20
I 32
O
I 11
O
O
O
I 100
I 66
O

Sample Output:

20 5
32 1
11 3
ERROR
100 5

题意:

给你两个栈,模拟一个队列,并且给出每次deque操作的时间。

AC代码:

#include <bits/stdc++.h>

#define all(s) s.begin(), s.end()

using namespace std;

const int N = 1e5 + 7;

string op;
int n, x, k;
stack<int> s1, s2;

int main() {
    cin.tie(nullptr)->ios::sync_with_stdio(false);
    cin >> n;
    while (n -- ) {
        cin >> op;
        if (op == "I") {
            cin >> x;
            s1.push(x);
        } else {
            if (s1.empty() && s2.empty()) {
                cout << "ERROR\n";
                continue;
            }
            if (s2.empty()) {
                while (!s1.empty()) {
                    s2.push(s1.top());
                    s1.pop();
                    k += 2;
                }
            }
            cout << s2.top() << ' ' <<  ++ k << endl;
            k = 0;
            s2.pop();
        }
    }
    return 0;
}

A-3 Rank of Binary Tree

Here we define the rank of a binary tree as n1​×n2​/n0​ where ni​ is the number of nodes of degree i for i=0,1,2.
For example, given a tree shown by the figure, its rank is 2×3/4=1.5.

Given the inorder and preorder traversal sequences of a binary tree, you are supposed to calculate its rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤20), which is the total number of nodes in the tree. Then given in the following 2 lines are the inorder and preorder traversal sequences of the tree, respectively. All the keys in the tree are distinct positive integers in the range of int.

Output Specification:

For each case, print in a line the way we calculate the rank, and the integer part of the rank. The format is:

n1 * n2 / n0 = rank

Sample Input:

9
2 3 1 5 4 7 8 6 9
1 2 3 6 7 4 5 8 9

Sample Output:

2 * 3 / 4 = 1

题意:

给你一颗二叉树的先序遍历和中序遍历,求每个顶点的度。

思路:

递归建树,再进行从根节点dfs计算每个节点的度,注意节点范围在int,因此最好开个map。

#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 20 + 7;
 
int n;
int inOrder[N], preOrder[N];
unordered_map<int, int> mp, degree;
 
struct TreeNode {
    int val;
    TreeNode *left, *right;
    TreeNode(int x): val(x), left(nullptr), right(nullptr) {}
};
 
TreeNode *build(int inL, int inR, int preL, int preR) {
    if (inL > inR) return nullptr;
    int curRoot = preOrder[preL];
    auto *root = new TreeNode(curRoot);
    int k = mp[curRoot];
    root->left = build(inL, k - 1, preL + 1, preL + k - inL);
    root->right = build(k + 1, inR, preL + k - inL + 1, preR);
    return root;
}
 
void DFS(TreeNode *root) {
    if (!root) return ;
    if (root->left) {
        degree[root->val] ++;
        DFS(root->left);
    }
    if (root->right) {
        degree[root->val] ++;
        DFS(root->right);
    }
}
 
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> inOrder[i], mp[inOrder[i]] = i;
    for (int i = 1; i <= n; i++) cin >> preOrder[i];
    TreeNode *root = build(1, n, 1, n);
    DFS(root);
    int n0 = 0, n1 = 0, n2 = 0;
    for (auto [k ,v] : mp) {
        if (degree[k] == 0) n0 ++;
        if (degree[k] == 1) n1 ++;
        if (degree[k] == 2) n2 ++;
    }
    cout << n1 << " * " << n2 << " / " << n0 << " = " << (int)(n1 * 1.0 * n2 / n0);
    return 0;
}

A-4 Big Number

How to generate a big number of N digits randomly? One way is to find N kids, give each one a card with one's index written on one side (hence it is assumed that the kids are indexed from 1 to N), and ask them to write down a 1-digit number randomly on the other side. Then let the kids pin their digits in a line, on the wall, one by one in ascending order of their indices.

However, it's very difficult to let hundreds of thousands of kids to follow the order. The result is that we have cards pinned randomly all over the wall, some even show the wrong sides. For example, if the 23rd kid has written down 8, we are supposed to find the number 8 on the wall. But instead we might find 23... Your job is to rearrange these cards so that we can obtain the big number as required.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤105). Then N lines follow, each describes a card in the format n1 n2 where the two numbers are the numbers written on the two sides of a card.

Output Specification:

For each test case, print in a line the N-digit number as required. That is, print the digits written by the kids in ascending order of their indices. In case that there are 1-digit numbers written on both sides, it would be hard to tell which one is the index and which one is the number written by the kid. Hence the solution may not be unique. In this case, just output the smallest resulting number.

It is guaranteed that a solution exists.

Sample Input:

12
7 11
8 9
3 1
2 12
4 6
10 0
5 1
2 5
6 8
1 4
7 2
9 3

Sample Output:

359114268072

这题当时考场上读了1小时没读懂题目,赛后问队友才明白题意。

题意:

给你n张卡牌,卡牌的一面写着索引(1-n)一面写着一位数的数字,这意味着若卡牌的一面为大于等于10的数字,则这个卡牌的索引和数字是确定的,若两面都为小于10的数字,则两面都可以互用,要求你按照索引给出最后拼成的最小的数字。

思路:

索引为10~n的数字是确定的,那么我们只考虑索引为1~9的即可,对于每个索引为1~9的我们找出当前可以放置的数字最小的,然后再把用过的这个边删掉即可。(这样的思路是错误的,这样贪心着放置数字可能会出现不合法的情况,例如当前位置选了当前最优的,删除后导致后面的索引没有数字可选了,也就是测试点2,3会报段错误)

 (26分)

#include <bits/stdc++.h>

#define all(s) s.begin(), s.end()

using namespace std;

const int N = 1e5 + 7;

int n, a, b;
vector<int> g[N];

int main() {
    cin.tie(nullptr)->ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a >> b;
        if (a >= 10) swap(a, b); //索引是 y 数字是x
        if (a < 10 && b < 10) {
            g[a].push_back(b), g[b].push_back(a);
        } else if (a < 10 && b >= 10) {
            g[b].push_back(a);
        }
    }
    for (int i = 1; i <= n; i++) {
        if (i >= 10) cout << g[i][0];
        else {
            int minv = *min_element(all(g[i]));
            g[minv].erase(find(all(g[minv]), i));
            cout << minv;
        }
    }
    return 0;
}

正解:

爆搜出所有合法的序列中的最优解。

AC代码

#include<bits/stdc++.h>

using namespace std;
vector <pair<int, int>> rec;
int n;
vector<int> getN(10, 0);
vector<int> ans0;
vector<int> temp;

bool greaterThan(vector<int> &a, vector<int> &b) {
    for (int i = 1; i < a.size(); i++) {
        if (a[i] != b[i]) {
            return a[i] > b[i];
        }
    }
    return false;
}

void dfs(int i) {
    if (i == rec.size()) {
        if (count(getN.begin() + 1, getN.begin() + rec.size() + 1, 0) == 0) {
            for (int j = 0; j < rec.size(); j++) {
                temp[rec[j].first] = rec[j].second;
            }
            if (ans0.size() == 0 || greaterThan(ans0, temp)) {
                ans0 = temp;
            }
        }
    } else {
        getN[rec[i].first]++;
        dfs(i + 1);
        getN[rec[i].first]--;
        swap(rec[i].first, rec[i].second);
        getN[rec[i].first]++;
        dfs(i + 1);
        getN[rec[i].first]--;
    }
}

int main() {
    cin >> n;
    vector<int> ans(n + 1, -1);
    for (int i = 0; i < n; i++) {
        int t1, t2;
        cin >> t1 >> t2;
        if (t1 < 10 && t2 < 10) {
            rec.emplace_back(t1, t2);
        } else {
            ans[max(t1, t2)] = min(t1, t2);
        }
    }
    temp.resize(rec.size() + 1);
    dfs(0);
    for (int i = 1; i < ans0.size(); i++) {
        ans[i] = ans0[i];
    }
    for (int i = 1; i < ans.size(); i++) {
        cout << ans[i];
    }
    return 0;
}

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

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

相关文章

SpringBoot 项目部署笔记

1. 直接通过 jar 包部署 本地直接 build package 成 jar 包&#xff0c;上传至服务器 ps -ef|grep XXX #查找项目进程sudo kill -9 19727 #杀掉项目进程nohup sudo java -jar *.jar >> app.log & #后台运行 jar &代表让该命令在后台执行 3. 通过 Jenkins …

pytorch笔记:conv2d

来自B站视频&#xff0c;API查阅&#xff0c;TORCH.NN nn.conv2d 中一般 kernel_size 是小奇数&#xff0c;padding 设置为 k − 1 2 \frac{k-1}{2} 2k−1​&#xff08;实际上padding的是 k − 1 k-1 k−1&#xff0c;因为参数的意义是左右各padding&#xff09;&#xff0c;

探索Xiotman:物联网软件架构的创新之路

文章目录 探索Xiotman&#xff1a;物联网软件架构的创新之路什么是物联网什么是XiotmanXiotman的特点Xiotman的架构Xiotman的使用安装env工具获取源代码使用其他教程 Xiotman的开源地址 总结 探索Xiotman&#xff1a;物联网软件架构的创新之路 什么是物联网 &#x1f680;&am…

高燃盛会全程回顾|鸿雁加速推进数字转型之路

6月10日&#xff0c;以“双翅齐振雁南飞”为主题的鸿雁电器数字化营销启动大会暨中山古镇鸿雁全屋智能体验中心开业庆典&#xff0c;在中山古镇华艺广场圆满落幕。 古镇镇长阮志力、华艺集团董事长区锦标、华艺广场总经理丁瑜、古镇灯饰传媒董事长曹利晖以及杭州鸿雁电器有限公…

spring boot + xxl-job 分布式任务调度

一、介绍 1、任务调度 1.1、什么是任务调度 我们可以先思考一下下面业务场景的解决方案&#xff1a; 某电商系统需要在每天上午10点&#xff0c;下午3点&#xff0c;晚上8点发放一批优惠券。某财务系统需要在每天上午10点前结算前一天的账单数据&#xff0c;统计汇总。某电…

那年我头脑发热,选择了自动化,后来我掉入计算机的世界无法自拔

首先&#xff0c;小雅兰是22届高考考生&#xff0c;而且当时填报志愿也没有填报到计算机相关的专业去&#xff0c;小雅兰是自动化专业的学生&#xff0c;是由于一次偶然的机会&#xff0c;了解到了这个行业&#xff0c;对于写代码所带来的成就感&#xff0c;总之&#xff0c;我…

Java013——常见进制以及转换

一、常见进制 十进制 数字组成&#xff1a;0-9这十个数字组成&#xff0c;不能以0开头 进位规则&#xff1a;满10进1 int num1 10;//对应的十进制为10二进制 数字组成&#xff1a;0-1这两个数字组成&#xff0c;以0b或0B开头 进位规则&#xff1a;满2进1 int num2 0b1010;…

华为存储IA篇仿真器搭建

设备清单 编号 设备名 数量 备注 01 Windows系统主机 1台 为VMware提供安装位置 02 VMware软件 1份 提供存储仿真器的部署环境 03 仿真器文件 1份 用于部署estor虚拟机 【注意】&#xff1a;暂无注意事项 一、下载安装文件并配置虚拟机设备清单 1.1…

TypeScript 自定义装饰器

&#xff08;预测未来最好的方法就是把它创造出来——尼葛洛庞帝&#xff09; 装饰器 装饰器一种更现代的代码模式&#xff0c;通过使用的形式注入在属性&#xff0c;寄存器&#xff0c;方法&#xff0c;方法参数和类中&#xff0c;比如在Angular&#xff0c;Nestjs和midway等…

大数据Doris(三十九):Spark Load 注意事项

文章目录 Spark Load 注意事项 Spark Load 注意事项 1、现在Spark load 还不支持 Doris 表字段是String类型的导入,如果你的表字段有String类型的请改成varchar类型,不然会导入失败,提示 type:ETL_QUALITY_UNSATISFIED; msg:quality not good enough to cancel 2、使用Spa…

无敌!我用【C语言】手搓出了一个体系完整的【员工管理系统】还能玩游戏听音乐?(超详细,附完整源码)

博主简介&#xff1a;Hello大家好呀&#xff0c;我是陈童学&#xff0c;一个与你一样正在慢慢前行的人。 博主主页&#xff1a;陈童学哦 所属专栏&#xff1a;C语言程序设计实验项目 如果本文对你有所帮助的话&#xff0c;还希望可以点赞&#x1f44d;收藏&#x1f4c2;支持一下…

Linux——IP协议1

目录 协议头格式 如何封装和解包 如何交付&#xff08;分用&#xff09; 报头每一个字段 分片是怎么做到的 应用层解决的是数据使用的问题。 在传输层&#xff0c;网络层&#xff0c;数据链路层&#xff1a;解决的是网络通信的细节&#xff0c;将数据可靠的从A主机跨网络发…

【深入浅出 Spring Security(八)】前后端分离-使用CSRF漏洞保护详讲

CSRF 漏洞保护 一、CSRF 概述二、CSRF 攻击演示三、CSRF 防御令牌同步模式 四、前后端分离使用 CSRFCsrfFilter 源码分析源码一些方法的细究 测试 五、总结 一、CSRF 概述 CSRF&#xff08;Cross-Site Request Forgery 跨站请求伪造&#xff09;&#xff0c;也可称为一键式攻击…

乐盟互动申请纳斯达克IPO上市,募资2000万美元

来源&#xff1a;猛兽财经 作者&#xff1a;猛兽财经 猛兽财经获悉&#xff0c;来自北京的程序化广告平台【乐盟互动】近期已向美国证券交易委员会&#xff08;SEC&#xff09;提交招股书&#xff0c;申请在纳斯达克IPO上市&#xff0c;股票代码&#xff08;LIAI&#xff09;&…

SpringBoot社区小区物业管理停车场系统(Java+Layui+MyBatis+Python+Mysql)

wx供重浩&#xff1a;创享日记 对话框发送&#xff1a;69小区 获取完整源码源文件说明文档数据库文件 项目特色 本项目使用现行主流技术与架构模式&#xff08;控制层、服务层、数据层&#xff09;代码结构清晰&#xff0c;严格遵循模块化、组件化、接口化思想&#xff1b;关…

mysq在RR级别怎么解决不可重复读和幻读

1、定义 不可重复读&#xff1a; 事务1读取一行&#xff0c;事务2然后修改或删除该行数据并且提交事务&#xff0c;事务1再次读取结果不一样&#xff1b; 幻读&#xff1a;事务1按条件读取查询数据&#xff0c;事务2按照同样的条件新增一条或多条数据并且提交事务&#xff0c…

mysql8查看大事务

文章目录 1、查看大事务的原因2、构建测试数据3、模拟大事务场景4、查询mysql的事务5、查询大事务的详情 1、查看大事务的原因 大事务的特点是执行时间长&#xff0c;长期占有锁不释放&#xff0c;导致其他想操作同一行数据的线程阻塞&#xff0c;如果客户端设置了超时时间&am…

单正态总体和双正态总体的假设检验

1.单正态总体和双正态总体的假设检验 笔者之前的相关笔记&#xff1a; 1.正态总体下常见的抽样分布 2.假设检验&#xff08;Hypothesis Testing&#xff09; 个人理解假设检验&#xff1a;先对总体参数提出一个假设值&#xff0c;利用样本信息判断这一假设是采取拒绝该假设还是…

opencv人与摄像头距离检测

参考&#xff1a; https://chtseng.wordpress.com/2018/09/18/%E5%A6%82%E4%BD%95%E4%BC%B0%E7%AE%97%E5%89%8D%E6%96%B9%E4%BA%BA%E7%89%A9%E7%9A%84%E8%B7%9D%E9%9B%A2/ WeChat_20230611160620 1、cv2加载摄像头慢解决方法&#xff0c;单独重新cv2.VideoCapture() https://b…

使用vue进行Lodop打印的一些方法

文章目录 使用Lodop进行打印的一般步骤vue中使用lodopkr-print-designer简介打印模板设计器打印预览模板设计页面安装引入 Lodop是一个JavaScript控件&#xff0c;用于在Web应用程序中进行打印操作。 使用Lodop进行打印的一般步骤 下载Lodop控件&#xff1a;首先&#xff0c;你…