![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
PAT刷题笔记
Offer.harvester
这个作者很懒,什么都没留下…
展开
-
给出一个整数,求解该整数各位之和。
第一方法:使用string存储该数字,然后遍历该数组所有位,进行累加求和。int sum(string num){ int sum_num; for(int i=0;i<num.length();i++){ sum_num+=num[i] - '0'; } return sum_num;}第二种方法:利用递归的方法进行求解。int sum(int n){ if(n==0) return 0; else return sum(n/10)+n%10;}若在学到其原创 2020-09-04 15:01:37 · 615 阅读 · 0 评论 -
1147 Heaps 我出错的地方,及其改正
在判断是否符合堆的特性时,我一开始使用的判断语句是:if ((heap[root] > heap[2 * root] || heap[root] > heap[2 * root + 1]) && (heap[2 * root]!= -999999 && heap[2 * root + 1]!= -999999)) flag_min = 0;//不是小根堆;虽然我将每个空节点都设置为了-999999.但是这样有一个致命的错误,就是root*2可能超过设置原创 2020-09-03 20:37:25 · 237 阅读 · 0 评论 -
1153 Decode Registration Card of PAT 最后两个测试点
不使用printf最后两个测试点会过不去,将COUT换成printf后就可以通过了。#include<bits/stdc++.h> #include<unordered_map>using namespace std;struct stu { string NO; int score; stu(string a,int b):NO(a),score(b){}};struct exam_room { string NO; int stu_num; exam_ro原创 2020-08-31 22:19:13 · 520 阅读 · 0 评论 -
1152 Google Recruitment【及其简单的一道题,说说一些注意的测试点】
1152 Google Recruitment (20分)OJ入口注意的点:1、在遍历输入的字符串中,注意边界的处理,不要忘记等号。2、输出的时候注意00004这种情况,前面的0也是要输出的。#include<bits/stdc++.h>using namespace std;string num;bool isprime(int num){ if(num==1||num == 0) return false; for(int i=2;i*i<=num;i++){原创 2020-08-20 18:59:39 · 461 阅读 · 2 评论 -
Telefraud Detection
7-3 Telefraud Detection (25 分)Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting victims lose their entire life savings. To stop this crime, you are supposed to write a program to detect those suspects from原创 2020-07-23 21:36:32 · 223 阅读 · 0 评论 -
vector数组之间的较量之<,>,==
在各大OJ算法题中我们经常会遇到一些最优结果的求解。例如下面这种要求:就是说数组A[]<B[],当且仅当A[i] == B[i] (i=0,1,2,3…L-2,L-1);而A[L]<B[L]。emmmmm大家可以联想一下字符串的字典序进行比较,就是你把数组看成一个字符串,这样就可以利用字典序进行比较啦。这种做法一般都是直接写个函数进行判断,但是很麻烦有木有,下面给大家介绍一下vector数组之间的比较符。利用vector可以直接判断两个数组是否相等,判断两个数组之间的大小关系。例如原创 2020-07-23 09:33:53 · 1245 阅读 · 1 评论 -
小学生,高中生,大学生,我是如何写gcd()最大公约数的
小学生写法:int gcd(int a, int b) { int r; while (b > 0) { r = a % b; a = b; b = r; } return a;}高中生写法int gcd_(int a, int b) { if (a%b == 0) return b; return gcd_(b, a%b);}大学生写法int gcd__(int a, int b) { return a % b == 0 ? b : gcd__(b, a%原创 2020-07-22 23:03:26 · 197 阅读 · 1 评论 -
【PAT】1089 Insert or Merge 24分?测试点二?测试点三?
【PAT】1089 Insert or Merge 针对 24分、测试点二来说说我的遭遇思想:暴力点吧,每排一次就比较一次。这里说一下vector可以直接使用=号来判断两个数组是否相同。还有就是插入排序和归并排序,在做OJ的时候大家就不必要直接实现它们了,可以对数组的部分元素进行sort就可以了,可以减少大家大量的代码量,思考量。例如:插入排序的插入操作不就可以直接利用sort进行吗?还有归并排序,进行两两归并的时候不是也可以直接sort一下吗?关于测试点二:大家记住啦,插入排序是从第二个开始原创 2020-07-22 20:03:31 · 410 阅读 · 0 评论 -
【PAT】1090 Highest Price in Supply Chain
测试点三:英爱是只有一个节点的情况,这个时候我们的sum应该为0。所以记录层数的变量应该初试值就是0,不能设置为1。#include<bits/stdc++.h>using namespace std;vector<int> tree[100003];//sum = 0 就是测试点三所考察的点int max_level = 0,sum=0;void DFS(int v,int n) { if (tree[v].size() == 0) {//树的叶子节点 if (原创 2020-07-22 16:51:25 · 196 阅读 · 0 评论 -
【PAT】1107 Social Clusters
OJ:【PAT】1107 Social Clusters思想:1、读入数据集的同时,将同一爱好的人存入同一个vector中。2、然后遍历不同的爱好vector,将他们一一求并集。3、然后遍历并查集数组father[]统计一共有多少个集合,每个集合中有多少人。#include<bits/stdc++.h>#include<unordered_map>using namespace std;int father[1004],cnt[1007];map<int,原创 2020-07-22 11:32:46 · 80 阅读 · 0 评论 -
【PAT】1106 Lowest Price in Supply Chain
#include<bits/stdc++.h>using namespace std;int N,num,temp;double P, r,minPrice;bool visited[100007];vector<int> v[100007];int minHeight = 999999,sum;void dfs(int root, int h) { if (v[root].size() == 0) { if (h == minHeight) { su..原创 2020-07-22 11:17:11 · 91 阅读 · 0 评论 -
【PAT】1102 Invert a Binary Tree
水题:正常输入树的结构,然后利用后序转换一下树的左右节点,在中序和层序遍历一下就好了树的转换,利用后序前序,后序都可以,后序是符合逻辑的。前序转换虽然不符合逻辑,但是结果也是对的。#include<bits/stdc++.h>using namespace std;typedef struct BiNode{ int lchild,rchild; }BiNode,*BiTree;int visited[10]={0};BiNode T[10];vector<int&g原创 2020-07-22 10:35:44 · 89 阅读 · 0 评论 -
【PAT】1101 Quick Sort 测试点2
2020/7/22OJ:【PAT】1101 Quick Sort1、 解题思想min_num[]记录每个元素右边的(包含本元素)最小值。2、max_num[]记录每个元素左边的(包含本元素)的最大值。3、max_num[i]表示第i个元素左边的元素都小于max_num[i]4、min_num[i]表示第i个元素右边的元素都大于min_num[i]然后遍历max_num[]和min_num[] max_num[i]=min_num[i]说明原数组中第i个元素就是pivot。原因如下:记 p原创 2020-07-22 10:27:09 · 287 阅读 · 0 评论 -
BST的操作自我总结笔记
typedef struct BiNode { int data; struct BiNode *lchild, *rchild;} *BiTree;插入void Insert(int key,BiTree &root) { if (root == NULL) { root = (BiNode*)malloc(sizeof(BiNode)); root->lchild = root->rchild = NULL; root->data = key; r原创 2020-07-21 11:47:58 · 343 阅读 · 0 评论 -
没写注释,三天后这个代码就不是我的了,。
#include<bits/stdc++.h>#include<unordered_map>#include<unordered_set>using namespace std;int father[10000];struct Node { int estate_num; int estate_area;};struct ansNode { int no; int num; double avg_sets; double avg_area;}a原创 2020-07-21 10:33:49 · 168 阅读 · 0 评论 -
【PAT】1112 Stucked Keyboard
【PAT】1112 Stucked Keyboard OJ入口注意:测试点一 aaaaa_cccc_xxx,a,c连续出现的次数并非K的倍数,因此是好键,x为坏键注意是k的倍数。 找了好久,大家接着加油。我的AC代码:思想:双指针,就是i和j在str上一起走,走啊走 实时判断连续出现的字符进行处理。注意输出的时候也要利用双指针进行输出。#include<bits/stdc++.h>#include<unordered_map>using namespace原创 2020-07-20 23:20:06 · 115 阅读 · 0 评论 -
【PAT】1119 Pre- and Post-order Traversals 思路加测试点讲解
思路讲解在我的另外一个博客中:【后序遍历】+【先序遍历】与【中序遍历】的关系关于有几个测试点出错AC代码:#include<bits/stdc++.h>using namespace std;const int MAXN = 31;int N,pre[MAXN],post[MAXN];struct BiNode { int left, right; BiNode() { left = 0; right = 0; }}BiTree[100];//l1 h1表示pre的上原创 2020-07-20 17:30:26 · 336 阅读 · 0 评论 -
【后序遍历】+【先序遍历】与【中序遍历】的关系 【PAT】1119 Pre- and Post-order Traversals
111原创 2020-07-20 17:26:36 · 884 阅读 · 0 评论 -
Dijkstra+DFS【解题模板】 + 【PAT】1003 Emergency
本题可以AC PAT 1003 Emergency OJ入口由于在Dijkstra算法中进行进行求解第二标尺的最优值时经常会由于逻辑复杂而浪费很多时间,所以,一般做题的时候为了思路清晰,逻辑清晰常常将求接最短路径和求解最优第二标尺路径分开进行。也就是先使用Dijkstra进行求解最短路径生产树。再DFS所有的最短路径求解出符合题目要求的最优路径。//迪杰斯特拉算法求出最短路径生成树void Dijkstra(int start) { fill(dist, dist + MAX, INF); d原创 2020-07-19 20:30:14 · 168 阅读 · 0 评论 -
【PAT】1148 Werewolf - Simple Version
#include<bits/stdc++.h>using namespace std;vector<int> v(101);int WH[101];//-1表示狼人,1表示人int N;//如果a,b,是狼人,判断是否符合要求,若符合要求就返回1,否则返回0;int Judge(int a, int b) { //令a,b都是狼人,其他人都是人 fill(WH, WH + 101, 1); WH[a] = WH[b] = -1; //接下来判断是否符合要求 in原创 2020-07-19 17:04:23 · 102 阅读 · 0 评论 -
Dijkstra算法的各种应用(附加边权、附加点权、计算最短路径条数)
功能数组const int MAXN = 1000;//图中最大领点个数const int INF = 0x3fffffff;int G[MAXN][MAXN], cost[MAXN][MAXN],weight[MAXN];//邻接矩阵存储图//迪杰斯特拉算法需要一个访问数组来记录集合S visited,距离数组dist,和一个前驱节点数组pre。bool visited[MAXN];//dist记录最短距离,pre记录前驱节点,c记录最小边花费,wg记录最大点累积,num记录最短路径数目原创 2020-07-19 10:19:52 · 1245 阅读 · 0 评论 -
【算法笔记】堆排序实现代码
//建堆时向下调整,或者删除元素进行向下调整void downAdjust(int root,int n){//root为当前欲调整的节点下标,n为节点总数 int i = root , j = i*2;//i为欲调整的节点,j为i的左节点 while(j<=n){ if(j+1<=n&&heap[j+1]>heap[j]) j++;//j = j+1 if(heap[i]<heap[j]){//当前节点比其子节点小那么进行交换 swap(he原创 2020-07-18 21:12:24 · 184 阅读 · 0 评论 -
并查集的【初始化】【查找】【合并】【路径压缩】
首先定义好存储并查集的数组const int N = 10000;int father[N];其中father[i]的值表示第i个节点的父节点,也就是说i节点属于father[i]所在的集合。如果father[i] == i那么就表示节点i是根节点,也就是说节点i是自己所在集合的根节点。并查集的初始化void initial(){ for(int i=0;i<N;i++){ father[i] = i; }}并查集的查找操作①、迭代实现int findFather(in原创 2020-07-18 19:56:19 · 559 阅读 · 0 评论 -
【PAT】1105 Spiral Matrix(柳婼的思想详细解读)
【PAT】1105 Spiral Matrix用的就是是柳大姐的思想:柳姐传送门先计算行数m和列数n的值,n从根号N的整数部分开始,往前推一直到1,找到第一个满足N % n== 0的,m的值等于N/n~将N个给定的值输入数组a,并将a数组中的值按非递增排序,接着建立m行n列的数组b,填充时按层数填充,一个包裹矩阵的口字型为一层,计算螺旋矩阵的层数level,如果m的值为偶数,层数为m/2,如果m为奇数,层数为m/2+1,所以level = m / 2 + m % 2;因为是从左上角第1个格子开始,按顺时原创 2020-07-17 15:50:25 · 678 阅读 · 0 评论 -
【C++算法基础】利用位运算来判断一个数的奇偶性
作者:刘扬俊原文链接:https://blog.csdn.net/qq_19782019/article/details/85621386今天看到一个大佬的快速幂算法,全程跪着看完的,大佬的优化一步一步榨干性能,从18秒到0秒的优化真的是让我跪着站不起来了。。。下面我做一个笔记记录一下我觉得比较实用的优化小技巧。在C语言中,power%2==1可以用更快的“位运算”来代替,power&1。因为如果power为偶数,则其二进制表示的最后一位一定是0;如果power是奇数,则其二进制表示的最原创 2020-07-17 09:27:05 · 1278 阅读 · 0 评论 -
【PAT】1143 Lowest Common Ancestor(测试点分析,普通思想,建树利用排序树的特点找公共祖先)
【PAT】1143 Lowest Common AncestorThe lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.A binary search tree (BST) is recursively defined as a binary tree which has the following properties原创 2020-07-17 08:48:31 · 531 阅读 · 2 评论 -
<ctype.h>函数大全 tolower()、toupper()、 isalnum()、isdigit()、isalpha()、islower().....
C 标准库 ----------------------------------<ctype.h>判断函数:1 int isalnum(int c)该函数检查所传的字符是否是字母和数字。2 int isalpha(int c)该函数检查所传的字符是否是字母。3 int iscntrl(int c)该函数检查所传的字符是否是控制字符。4 int isdigit(int c)该函数检查所传的字符是否是十进制数字。5 int isgraph(int c)该函数检查所传的字符是否有原创 2020-07-16 16:21:57 · 241 阅读 · 0 评论 -
【PAT】1022. Digital Library
A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are suppose原创 2020-07-03 10:50:40 · 112 阅读 · 0 评论 -
【PAT】1052 Linked List Sorting 测试点分析
题目A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structu原创 2020-06-28 22:41:57 · 676 阅读 · 0 评论 -
【PAT】1032 Sharing (25分)测试点3,测试点4,测试点5分析
OJ入口:1032题意:你以为题意是求共同后缀的首地址值吗?不不不不,我自己觉得这个地方题意有一点问题,其实就是找到两个单词的第一个共同节点。也就是说只在某一段区间两个串的值是相同的,然后后面的值又可能会不同。例如下图:测试点3,4在文章最后给出测试数据(自己设计的),大家可以测试自己的代码。我一开始就是以为求共同后缀的首地址值吗?也就是从后往前遍历找共同后缀的第一个相同的元素的地址。除了最后一个测试点(测试点五)没有通过,其他都通过了(后面给出AC代码),代码如下:#include<bi原创 2020-06-28 21:48:22 · 1301 阅读 · 5 评论 -
【PAT】 1101 Quick Sort(测试点二坑点分析之格式错误)
一、题意There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. Gi原创 2020-06-27 21:21:36 · 616 阅读 · 0 评论 -
【PAT】1136 A Delayed Palindrome(大整数加法及其测试点分析) 避坑指南
Consider a positive integer N written in standard notation with k+1 digits ai as ak ⋯a1 a0 with 0≤ai <10 for all i and ak >0. Then N is palindromic if and only if ai =ak−i for all i. Zero is written 0 and is also palindromic原创 2020-06-27 19:56:10 · 517 阅读 · 0 评论 -
【数学问题】大整数运算 加减比较
大整数运算,附上代码,实现了1000位以内的大整数运算,有大整数的加减,以及比较运算。#include<bits/stdc++.h>using namespace std;struct bignum{ int data[1000]; int len; bignum(){//结构体初始化函数 memset(data,0,sizeof(data)); len=0; }};//将String类型转换为int数组 bignum convert(string str){ b原创 2020-06-27 12:04:37 · 253 阅读 · 0 评论 -
【PAT】1061 Dating && 1014 福尔摩斯的约会 测试点分析
大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm。大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间星期四 14:04,因为前面两字符串中第 1 对相同的大写英文字母(大小写有区分)是第 4 个字母 D,代表星期四;第 2 对相同的字符是 E ,那是第 5 个英文字母,代表一天里的第 14 个钟头(于是一天的 0 点到 23 点由数字 0 到 9、以及大写字母 A 到 N 表示);原创 2020-06-22 10:15:51 · 547 阅读 · 1 评论 -
【PAT】1012 数字分类 (20 分)+测试点八的坑(刷题笔记)
给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字:A1 = 能被 5 整除的数字中所有偶数的和;A2 = 将被 5 除后余 1 的数字按给出顺序进行交错求和,即计算 n1−n2+n3−n4⋯;A3 = 被 5 除后余 2 的数字的个数;A4 = 被 5 除后余 3 的数字的平均数,精确到小数点后 1 位;A5 = 被 5 除后余 4 的数字中最大数字。输入格式:每个输入包含 1 个测试用例。每个测试用例先给出一个不超过 10原创 2020-06-21 21:45:29 · 309 阅读 · 0 评论