模拟题

PAT (Advanced Level) Practice 模拟题

目录

  • 1008 Elevator (20)
  • 1042 Shuffling Machine (20)
  • 1046 Shortest Distance (20)
  • 1051 Pop Sequence (25)
  • 1117 Eddington Number (25)
  • 1128 N Queens Puzzle (20)

1008 Elevator (20)

#include<cstdio>
int main()
{
int N;
scanf("%d",&N);
int currentfloor = 0;
int nextfloor;
int time = 5 * N;
for (int i = 0; i < N; i++){
scanf("%d", &nextfloor);
if (nextfloor > currentfloor) time += 6 * (nextfloor - currentfloor);
if (nextfloor < currentfloor) time += 4 * (currentfloor - nextfloor);
currentfloor = nextfloor;
}
printf("%d", time);
}

1042 Shuffling Machine (20)

#include<iostream>
#include<string>
using namespace std;
int main()
{
string oldcards[54];
string newcards[54];
for (int i = 0; i < 54; i++){
if (i < 13){
oldcards[i] = "S";
oldcards[i].append(to_string(i+1));
} else if (i < 26){
oldcards[i] = "H";
oldcards[i].append(to_string(i+1-13));
} else if (i < 39){
oldcards[i] = "C";
oldcards[i].append(to_string(i+1-26));
} else if (i < 52){
oldcards[i] = "D";
oldcards[i].append(to_string(i+1-39));
} else {
oldcards[i] = "J";
oldcards[i].append(to_string(i+1-52));
}
}
int n;
cin >> n;
int order[54];
for (int i = 0; i < 54; i++) cin >> order[i];
for (int k = 0; k < n; k++){
for (int i = 0; i < 54; i++)
newcards[order[i]-1] = oldcards[i];
for (int i = 0; i < 54; i++)
oldcards[i] = newcards[i];
}
cout << oldcards[0];
for (int i = 1; i < 54; i++){
cout << " " << oldcards[i];
}
}

1046 Shortest Distance (20)

题目思路

  • 所有结点连起来会形成一个环形,每次输入都重新加一遍距离会超时,即使记录sum每次只算一侧也会超时。
  • 用dis[i]存储第1个结点到第i个结点的下一个结点的距离,sum保存整个路径一圈的总和值。
  • 求得结果就是dis[right – 1] – dis[left – 1]和 sum – dis[right – 1] – dis[left – 1]中较小的那一个
  • 注意:输入两个数没有顺序规定,如果左大右小需要交换
    • 交换可使用swap函数,许多stl都实现了这一函数,可以用
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int sum = 0, N, M, a, b, temp, d;
scanf("%d",&N);
int distance[N+1] = {0};
for (int i = 1; i <= N; i++){
scanf("%d", &temp);
sum += temp;
distance[i] = sum;
}
scanf("%d", &M);
for (int i = 0; i < M; i++){
scanf("%d%d", &a, &b);
if (a > b) swap(a, b);
d = distance[b-1] - distance[a-1];
printf("%d\n", min(d, sum-d));
}
return 0;
}

1051 Pop Sequence (25)

栈模拟

  • 不管是否已经判断出可不可能,要先把输入的序列接收进来。所以开一个数组先用一趟循环接受本次所有输入。
  • 按顺序1~n把数字进栈,每进入一个数字,判断有没有超过最大范围
    • 超过了说明到此压栈弹栈操作使得栈内元素大于规定容量,break。
    • 如果没超过,检查是否需要弹出
      • 压栈循环外设置变量 current 记录检查到输入序列的第几个。
      • 每次将栈顶元素与输入序列检查到的对应位置是否相等,while相等则一直弹出且current后移。
      • 若栈顶元素与序列不符或栈已经弹空,则继续压栈新数字。
      • 注意:在while条件中要把!pop.empty()放在&&前,即先检查栈是否为空,否则在栈空时访问pop.top()是非法行为
  • 如果压栈弹栈操作始终使得栈容不超过规定值,应当会检查到输入序列的最后,若current没有到达n+1,说明有元素没有被检查到,栈未被弹空,应输出NO,反之输出YES
#include<cstdio>
#include<stack>
using namespace std;
int main()
{
int m, n, k;
scanf("%d%d%d", &m, &n, &k);
for (int i = 0; i < k; i++){
int seq[n+1];
stack<int> pop;
for (int j = 1; j < n+1; j++) scanf("%d", seq+j);
int current = 1;
for (int j = 1; j < n+1; j++){
pop.push(j);
if (pop.size() > m) break;
while (!pop.empty() && pop.top() == seq[current]){
pop.pop();
current++;
}
}
if (current < n+1) printf("NO\n");
else printf("YES\n");
}
return 0;
}

1117 Eddington Number (25)

题目思路

  • 在数组中存储n天的公里数,从大到小排序
  • i+1 表示了第几天骑车,那么满足 dis[i] > i + 1 的最大 i 即为所求
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n, i;
scanf("%d", &n);
int dis[n];
for (i = 0; i < n; i++) scanf("%d", &dis[i]);
sort(dis, dis+n, greater<int>());
for (i = 0; i < n; i++)
if (dis[i] <= i + 1) break;
printf("%d\n", i);
return 0;
}

1128 N Queens Puzzle (20)

题目思路

  • 不管是否已经判断出是否为解,要先把输入的序列接收进来。所以开一个数组先用一趟循环接受本次所有输入。
  • 对于第j个数字,判断其前输入的数字中是否有在同一行的 seq[j] == seq[k] 和在斜对角线上的 abs(seq[j]-seq[k]) == abs(j-k)
  • 不满足改变标记变量break离开检查循环,根据标记变量进行对应输出
  • 简化代码:若不满足标记变量不继续进行循环,可将标记变量写入for循环条件。
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int K, n;
scanf("%d", &K);
for (int i = 0; i < K; i++){
bool issolution = true;
scanf("%d", &n);
int seq[n+1] = {0};
for (int j = 1; j < n+1; j++) scanf("%d", seq+j);
for (int j = 1; j < n+1 && issolution; j++){
for (int k = 1; k < j; k++){
if (seq[j] == seq[k] || abs(seq[j]-seq[k]) == abs(j-k)){
issolution = false;
break;
}
}
}
printf("%s", issolution ? "YES\n" : "NO\n");
}
return 0;
}

PAT甲级 模拟题_C++题解的更多相关文章

  1. PAT甲级 排序题_C++题解

    排序题 PAT (Advanced Level) Practice 排序题 目录 <算法笔记> 6.9.6 sort()用法 <算法笔记> 4.1 排序题步骤 1012 The ...

  2. PAT甲级 链表题_C++题解

    链表处理 PAT (Advanced Level) Practice 链表题 目录 <算法笔记> 重点摘要:静态链表 1032 Sharing (25) 1052 Linked List ...

  3. PAT甲级真题及训练集

    正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...

  4. 1080 Graduate Admission——PAT甲级真题

    1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...

  5. PAT甲级2019冬季考试题解

    A Good In C纯模拟题,用string数组读入数据,注意单词数量的判断 #include<bits/stdc++.h> using namespace std; ; ][]; in ...

  6. PAT甲级 散列题_C++题解

    散列 PAT (Advanced Level) Practice 散列题 目录 <算法笔记> 重点摘要 1002 A+B for Polynomials (25) 1009 Product ...

  7. PAT甲级 Dijkstra 相关题_C++题解

    Dijkstra PAT (Advanced Level) Practice Dijkstra 相关题 目录 <算法笔记>重点摘要 1003 Emergency (25) <算法笔记 ...

  8. PAT甲级 进制转换题_C++题解

    进制转换题 PAT (Advanced Level) Practice 进制转换题 目录 <算法笔记> 重点摘要 1015 Reversible Primes (20) 1019 Gene ...

  9. PAT甲级 二叉树 相关题_C++题解

    二叉树 PAT (Advanced Level) Practice 二叉树 相关题 目录 <算法笔记> 重点摘要 1020 Tree Traversals (25) 1086 Tree T ...

随机推荐

  1. P3956 棋盘——普及题,儿童搜索

    P3956 棋盘 这道搜索弄得我很难受. 第一,一定要看清楚题在写.第二,弄清楚判断条件: 首先图的大小是m*m不是n*m; 然后就是当前有颜色的点是不用变颜色的: #include<cstdi ...

  2. 1、套按字及http基础知识之一

    MAC地址:设备到设备之间通信时专用(从源主机到目标主机可能经由N台路由设备)4 IP地址:标记主机到主机之间通信时专用 TCP/UDP :提供进程地址 通过port number来标记 进程地址:用 ...

  3. Linux后台运行任务

    [Linux]ssh命令行下多任务前后台切换 原文:https://my.oschina.net/huxuanhui/blog/13844 我们都知道,在 Windows 上面,我们要么让一个程序作为 ...

  4. 反向代理Nginx

    引用:https://baijiahao.baidu.com/s?id=1600687025749463237&wfr=spider&for=pc 参考下图,正向代理用途:Client ...

  5. python小白之np功能快速查

    np一些用法 np.a np.array([1,2,3],dtype=int)  #建立一个一维数组, np.array([[1,2,3],[2,3,4]])  #建立一个二维数组. np.arang ...

  6. RabbitMQ 3.7.X集群:从入门到精通,这一篇就够了

    RabbitMQ是流行的开源消息队列系统,本身已经具备了较强的并发处理速度及运行稳定性,然而在大规模的实际应用中,往往还需要使用集群配置来保证系统中消息通信部分的高可用性,并发处理性能及异常恢复能力. ...

  7. utf-8的中文是一个字符占几个字节

    utf-8的中文是一个字符占几个字节 英文字母和中文汉字在不同字符集编码下的字节数英文字母:·字节数 : 1;编码:GB2312 字节数 : 1;编码:GBK 字节数 : 1;编码:GB18030 字 ...

  8. 41 Flutter 仿京东商城项目签名验证 增加收货地址、显示收货地址 事件广播

    加群452892873 下载对应41课文件,运行方法,建好项目,直接替换lib目录 AddressAdd.dart import 'package:dio/dio.dart'; import 'pac ...

  9. VCL组件之TPanel

    TPanel位于Standard组件面板上,也是常用的一种容器控件.面板的一个优点就是放在面板上的组件称为面板的一部分,因此它们与面板一起移动.这在设计阶段很有用. Panel组件的大部分功能在于其A ...

  10. MySQL中使用replace into语句批量更新表数据

    作为示例,我们在这里使用名为testdb的数据库,并且在其中创建两张一模一样的表: drop table if exists test_table_1; create table test_table ...