- 博客(20)
- 资源 (3)
- 收藏
- 关注
原创 多重背包
int main(){ int n,m; int result[100][100]; int weight[100],value[100],amount[100]; int newweight[100],newvalue[100]; scanf("%d%d",&n,&m); int number=0; for(int i=1;i<=n;i++){ scanf("%d%d%d",&weight[i],&
2021-03-04 20:44:40
195
1
原创 01 背包与完全背包(动态规划)
int main(){ int n,m; int result[100][100]; int weight[100],value[100]; //物品数量,背包容量 scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ //第i个物品重量和价值 scanf("%d%d",&weight[i],&value[i]); } for(int i=0;i&l
2021-03-04 19:57:16
204
1
原创 最大递增子字符串长度
递归策略+记忆数组 int result; int arr[100]; //记忆数组,前面计算过的直接在这里查就行了,不用在递归里重复操作了,时间复杂度O(n^2) int memo[100]; //返回以arr[n]结尾的最长子字符串长度 int F(int n){ if(memo[n]!=-1) return memo[n]; if(n==0) result=1; else{ result=1; for(int i=0;i<n;i++){
2021-03-04 17:30:11
244
1
原创 关键路径长度和结点最早最晚开始时间
#include <iostream> #include <vector> #include <queue> #include <stdio.h> #include <string> #include <cstring> using namespace std; struct Edge{ int to; int length; Edge(int t,int l):to(t),length(l){} }; vect
2021-03-04 10:06:05
1022
1
原创 拓扑排序
c语言小白,写博客用来复习 #include<iostream> #include <vector> #include <queue> using namespace std; //赢了哪队 vector<int> ve[10]; //i的入度 int indegree[10]; //入度为0的进入队列再出 priority_queue<int,vector<int>,greater<int>> node; vector&l
2021-03-03 20:30:49
120
1
原创 最短路径(优先队列)
/* #include <iostream> #include <vector> #include <string> #include <cstring> using namespace std; struct Edge{ int length; int to; Edge(int to,int length):to(to),length(length){} }; vector<Edge> graph[10]; int dis
2021-03-03 19:21:46
435
1
原创 最短路径
#include <iostream> #include <vector> #include <string> #include <cstring> using namespace std; struct Edge{ int length; int to; Edge(int to,int length):to(to),length(length){} }; vector<Edge> graph[10]; int dis[10
2021-03-03 19:00:11
120
1
转载 图的广度优先遍历(邻接矩阵)
#include<stdio.h> #include<stdlib.h> //--------------------------------------以下是邻接矩阵部分------------------------------ //第一步骤,边的定义 struct Enode { int v1,v2; int weight; }; typedef struct Enode* Edge; //第二步骤,图的定义 struct Gnode { int Nv,Ne;
2021-03-03 15:33:18
1470
1
转载 最小生成树(prim)
#include<iostream> #include<string> #include<algorithm> using namespace std; #define INF (1<<21) int cost[500][500]; int dist[500]; bool used[500]; int v; int prim(){ for(int i=0;i<v;i++){ dist[i] = INF; u
2021-03-03 15:31:30
108
1
转载 最小生成树
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int n,m,tot=0,k=0;//n端点总数,m边数,tot记录最终答案,k已经连接了多少边 int fat[200010];//记录集体老大 struct node { int from,to,dis;//结构体储存边 }edge[200010]; bool cmp(const node &a,c
2021-03-03 15:30:22
91
1
原创 哈夫曼树(优先队列)
#include<iostream> #include <queue> #include <cstdio> using namespace std; int main() { int d,result,a,b; //大根堆,升序队列,先弹出最小的,不写默认大根堆降序 priority_queue<int,vector<int>,greater<int>> myQueue; for(int i=0;i<
2021-03-02 16:49:12
212
原创 二叉排序树的建立
#include<iostream> #include <string> using namespace std; struct TNode{ int data; TNode* lchild; TNode* rchild; TNode(int d): data(d),lchild(nullptr),rchild(nullptr) {} }; TNode* Tree; TNode* buildTree(int d,TNode* tr){ if(tr
2021-03-02 16:06:45
113
原创 二叉树的建立与遍历(c++)
#include<iostream> #include <string> using namespace std; struct TNode{ char data; TNode *left; TNode *right; }; TNode *Tree; int index; TNode *createTree(string str,int n){ TNode *T; if(index>=n) return (NULL); else
2021-03-02 11:17:38
269
原创 火柴(深度优先遍历)
n根火柴,判断其是否可以构成正方形 #include<iostream> #include <algorithm> using namespace std; int visit[100]; int sticks[100]; //完成边数,每边长度,该边已完成长度,一股几根火柴 bool DFS(int number,int side,int sum,int n){ if(number==4) return true; for(int i=0;i<n;i++){
2021-03-01 21:24:18
141
原创 全排列(递归c++)
#include<iostream> #include <algorithm> #include <string> using namespace std; //输出字符串的全排列 int visit[10]; char result[10]; void getPermution(string str,int index){ if(index==str.size()){ for(int i=0;i<str.size();i++){
2021-03-01 18:59:30
150
原创 贪心算法(c++)
普通贪心 gun:枪的伤害,monster:怪物的防御,伤害>防御才能杀死怪物,每支枪只能用一次,每只怪物只能死一次,奖金=伤害-防御,求最大奖金 //思路:每次循环都让伤害最高的枪打防御最低的怪物,打完后伤害归0,防御顶天。知道没有枪能打死怪物。 #include <iostream> #include <algorithm> using namespace std; long gun[1000]; long monster[1000]; int compare(long
2021-03-01 17:02:10
596
原创 KMP算法代码(c++)
c语言小白,写博客供自己复习。 #include <stdio.h> #include <iostream> #include <string> using namespace std; void Next(string t,int next[]) { int j =0,k = -1; next[0] = -1; while(j < t.length()-1) { if(k == -1||t[j]==t[k])
2021-02-28 16:16:56
340
原创 各种排序算法(c语言)
c语言小白,写文章方便复习记忆。 冒泡排序 从第一个元素开始,依次与后面元素进行比较,较大者放在后面,比较完一轮后,最大的在最后一个。然后再对除最大的元素外进行比较。 直接插入排序 a[0]作哨兵,将待插入的记录与有序区中的各记录自右向左依次比较其关键字值的大小,较大者放在后面,先比较前两个,依次增大范围,相当于插入。 快速排序 a[0]做哨兵,从后或前向中间遍历,保证后面的比他大,前面的比他小,否则将其放在相应位置并转到前或后继续遍历。 选择排序 将第一个元素与其他元素比较,小的放在第一个位置,这样前面的
2021-02-27 17:06:14
248
原创 DFS(c语言)
c语言小白,写文章方便自己记忆复习 #include<stdio.h> #include<stdlib.h> //边的定义 struct Enode { int v1, v2; int weight; }; typedef struct Enode* Edge; //图的定义 struct Gnode { int Nv, Ne; int** GL; }; typedef struct Gnode* Graph; int visit[100]; //图的初始化 Graph cr
2021-02-27 09:52:34
1339
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人