《算法笔记》读书笔记
一些笔记和题目代码
独自莫凭欄
。。
展开
-
Dijkstra(迪杰斯特拉)模板
Dijkstra(迪杰斯特拉)算法思想本质来说是一种贪心。每次选择一条最短路径,这样最后得到的就是一条最短路径。举个例子:代码模板(邻接表与邻接矩阵)邻接矩阵版#include <iostream>#include <algorithm>#include <cstring>using namespace std;const int MAXV = 1000;const int INF = 1e9;int n, G[MAXV][MAXV];/原创 2021-08-16 19:40:34 · 482 阅读 · 0 评论 -
PAT A.1103 Integer Factorization (DFS)
题目链接#include <bits/stdc++.h>using namespace std;int n, k, p, maxFacSum = -1;vector<int> fac, ans, temp;int power(int x){ int ans = 1; for(int i = 0; i < p; i++){ ans *= x; } return ans;}void Init(){ i.原创 2021-08-10 14:52:24 · 87 阅读 · 0 评论 -
PAT A.1020 Tree Traversals(给出二叉树后序和中序遍历求层序遍历)
PAT A.1020 Tree Traversals 层序遍历要借助队列,然后就是中序和后序可以得到前序 。懒得多说了反正书上都有#include <iostream>#include <algorithm>#include <queue>using namespace std;const int maxn = 100;struct node{ int data; node* Lchild; node* Rchild;}原创 2021-08-08 17:54:06 · 78 阅读 · 0 评论 -
PAT A.1042 Shuffling Machine
PAT A.1042 Shuffling Machine最近心态是真的不好,微信小程序老出bug我查资料都头疼了。主要没学过基础。真的烦。大创真的心累啊。没跟到大佬。继续加油吧。这题是一个模拟,并不涉及到洗牌的算法。但是我一开始老师报错,原来是把循环次数写错了,K写成了N。估计以后刷题时间也少了。希望一天一题能把PAT刷完,再acwing算法基础刷完就心满意足了。毕竟不是搞竞赛的。相比于代码题,还是更喜欢做数学题,哈哈。选错专业了。开个玩笑了。希望我心态更好吧。开心面对压力,#include &l原创 2021-08-05 15:30:08 · 78 阅读 · 2 评论 -
PAT A.1003 Emergency(迪杰斯特拉算法)
题目链接:PAT A.1003原创 2021-08-03 19:55:58 · 118 阅读 · 0 评论 -
埃氏筛法与欧拉筛法(素数筛)
埃氏筛法与欧拉筛法(素数筛)暴力解决引入问题首先我们的问题是怎么样把素数筛选出来。如何解决首先我们应该清除的是素数的定义素数:大于等于2的只能被自身和1整除的整数叫素数,又称作质数我们最容易想到的办法就是根据定义来求也就是暴力求解,迭代法bool isPrime(int n){ if(n<=1) return false; for(int i=2;i<n;i++){ if(n % i == 0){ return false; } } return t原创 2021-07-28 17:20:24 · 248 阅读 · 0 评论 -
PAT 1076 Forwards on Weibo
PAT 1076 Forwards on WeiboBFS广搜,解决,DFS也可以解决,但是更麻烦,有时间补充解方案#include <bits/stdc++.h>using namespace std;const int maxv=1010;struct node{ int id; int layer;};vector<node> Adj[maxv];bool inq[maxv]={false};int BFS(int s, int L){原创 2021-07-27 15:27:57 · 56 阅读 · 0 评论 -
PAT A.1034 Head of a Gang
PAT A.1034 Head of a GangPAT A.1034 Head of a Gang#include <bits/stdc++.h>using namespace std;const int maxn=2010;const int INF=1000000000;map<int,string> intToString;map<string,int> stringToInt;map<string,int> Gang;原创 2021-07-27 14:39:36 · 59 阅读 · 0 评论 -
数据结构——并查集
数据结构——并查集并查集的定义定义:并查集是一种维护集合的数据结构。是一种树型的数据结构,用于处理一些不相交集合的合并及查询问题(即所谓的并、查)。比如说,我们可以用并查集来判断一个森林中有几棵树、某个节点是否属于某棵树等。并查集支持下面两个操作:(1)合并:合并两个集合(2)查找:判断两个元素是否在一个集合并查集的表示其实就是一个数组例如:int Father[N]其中father[i]表示元素i的父亲结点,父亲结点本身也是这个集合内的元素并查集的基本操作1.初始化一开始的时候,原创 2021-07-25 14:17:43 · 109 阅读 · 0 评论 -
PAT A.1043 Is It a Binary Search Tree
题目链接:1043 Is It a Binary Search Tree#include <iostream>#include <algorithm>#include <vector>using namespace std;struct node{ int data; node *lchild; node *rchild; };void insert(node* &root,int data){ if(root==NULL) {原创 2021-07-24 18:28:19 · 100 阅读 · 0 评论 -
PAT 1030 完美数列
PAT 1030 完美数列#include <iostream>#include <algorithm>using namespace std;const int maxn=100010;int n,p,a[maxn];typedef long long LL;int main(){ cin>>n>>p; int i; for(i=0;i<n;i++){ cin>>a[i]; } sort(a,a+n);原创 2021-07-22 09:14:51 · 91 阅读 · 0 评论 -
PAT A.1032 Sharing
题目链接:PAT A.1032 SharingTo store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, loading and being are stored as原创 2021-07-21 11:22:30 · 55 阅读 · 0 评论 -
C++ STL之queue和priority_queue简单用法
C++ STL之queue简单用法1.queue定义queue<typename> name;2.queue内元素访问只能通front()访问队首元素或者back()访问队尾元素q.front();q.back();3.queue常用函数1.push()把元素入队尾2.pop()把队首元素出队//注意队列是先进先出3.empty()判断队列是否为空4.size()返回queue元素个数‘C++ STL之priority_queue简单用法1.优先队列的原创 2021-07-20 20:28:52 · 79 阅读 · 0 评论 -
C++STL之map简要用法
C++STL之map简要用法1.map定义map<typename1,typename2> mp;//例子:map<int string> mp;map<set<int>,string>mp;2.map内元素访问1.通过下标访问//注意map里面的键是唯一的#include <bits/stdc++.h>#include <map>using namespace std;int main(){ map原创 2021-07-20 20:02:16 · 96 阅读 · 0 评论 -
PAT A.1060 Are They Equal
题目描述If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123×105 with simple chopping. Now given the number of significant digits on a machine and two float numbers,原创 2021-07-20 19:09:26 · 46 阅读 · 0 评论 -
C++ STL之String的常见用法详解
C++ STL之String的常见用法详解1.String的定义string str;string str="abcd";2.string中的内容访问1.通过下标访问#include <bits/stdc++.h>using namespace std;int main(){ string str="abcde"; for(int i=0;i<str.length();i++){ cout<<str[i]; }原创 2021-07-20 18:38:09 · 190 阅读 · 0 评论 -
C++ STL之set常见用法详解
C++ STL之set常见用法详解1.set的定义set是一个内部自动有序的集合而且不含重复元素1.单独定义一个setset<typename> name;//例子set<int> name;set<node> name //node是结构体类型//和vector基本类似set数组:set<int> a[100]; 2.对set内元素的访问==注意set只能用迭代器访问==#include <bits/stdc++.h>原创 2021-07-20 14:32:28 · 249 阅读 · 0 评论 -
C++ STL之vector常见用法详解
C++ STL之vector常见用法详解1.vector的定义vector翻译为向量,在这里其实就是一种变长数组,也就是长度根据需要自己会发生变化1.定义方式: vector<typename> name; 这里的typename可以是任何基本类型,例如int,char,结构体等,也可以是STL标准容器 但是定义为STL标准容器时>>符号间记得加空格,如下: vector<int> name; vector<double> name; vec原创 2021-07-20 11:05:03 · 151 阅读 · 0 评论 -
PAT A.1033 To Fill or Not to Fill
题目链接:PAT -A.1033 To Fill or Not to Fill题目:With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station m原创 2021-07-18 15:45:09 · 65 阅读 · 2 评论 -
PAT A.1075 PAT Judge
题目叙述PAT 的排名列表是从状态列表中生成的,它显示了提交的分数。这次您应该为 PAT 生成排名列表。输入规格:每个输入文件包含一个测试用例。对于每种情况,第一行包含 3 个正整数,N (≤104 ),用户总数, 至 (≤5),问题总数,以及 米 (≤105 ),提交总数。然后假设用户 ID 是 5 位数字,从 00001 到N,问题 id 从 1 到 至. 下一行包含至正整数p[i]( i=1, …,至),其中p[i]对应第 i 个问题的满分。然后米 后面几行,每行都以以下格式原创 2021-07-17 12:18:46 · 102 阅读 · 1 评论 -
PAT——A.1025 PAT Ranking
题目叙述编程能力测试(PAT)由浙江大学计算机科学与技术学院主办。每个测试应该在多个地方同时运行,测试后将立即合并排名列表。现在您的工作是编写一个程序来正确合并所有排名列表并生成最终排名。输入规格:每个输入文件包含一个测试用例。对于每种情况,第一行包含一个正数N (≤100),测试位置的数量。然后N ranklists 紧随其后,每个都以一行包含一个正整数开始 至 (≤300),被试人数,然后 至包含注册号(一个 13 位数字)和每个测试者的总分的行。一行中的所有数字都用空格分隔。输出规格:对于每原创 2021-07-16 15:50:23 · 101 阅读 · 1 评论 -
PAT——A.1015—phone bills
原题目1016 Phone Bills (25 分)A long-distance telephone company charges its customers by the following rules:Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting原创 2021-07-16 14:09:09 · 67 阅读 · 1 评论 -
PAT A1012——The best Rank(最好排名)
算法笔记里面的题目就不多说了,反正是自己练习。供自己用的,留个纪念。学到一个新函数。round()。用来对数字四舍五入。round函数将返回一个指定格式的舍入整数,该整数将被舍入到最接近的整数。就是如果不加格式控制的话,返回一个四舍五入的整数。#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>usi.原创 2021-07-16 08:05:51 · 196 阅读 · 0 评论 -
PAT练习A.1062—德才分排序
题目叙述中文翻译这题目不是很难就是一个排序然后分类的问题,但是书上面的方法利用了sort函数的一些特性。sort后面可以比较函数。代码如下:#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct Student{ char id[10];//准考证 int de,cai,原创 2021-07-15 20:45:18 · 71 阅读 · 0 评论