随笔分类 - 算法
摘要:定义 使用father[i]表示i的父节点,当father[i]=i时,i为根节点。同一个集合仅有一个根节点,并将其作为根节点的标识 初始化 //初始化 //for (int i = 0; i < n; i++) //{ // father[i] = i; //} 查找 返回x所在集合的根节点 //
阅读全文
摘要:性质 对于AVL树的任意节点来说,左子树和右子树的高度之差的绝对值不超过1 二叉平衡树 //二叉平衡树 struct node { int data, height; node* left; node* right; }; 生成一个新节点 //生成一个新节点 node* newNode(int da
阅读全文
摘要:性质 左孩子<=结点<右孩子 二叉查找树 struct node { int data; node* left; node* right; }; 二叉查找树的查找 //二叉查找树的查找 void search(node*root,int v) { if (root == NULL) { //没找到
阅读全文
摘要:二叉树的链表存储结构 //二叉树的链表存储结构 struct node { int data;//数据域 node* lchild;//指向左子树根节点的指针 node* rchild;//指向右子树根节点的指针 }; 新建结点 //新建结点 node* newNode(int v) { node*
阅读全文
摘要:广度优先搜索小技巧 使用bool inq(in queue)来记录某一节点是不是入过队(而不是是否被访问) 矩阵的块 求给定的矩阵中块的个数 BFS解法 #include<queue> #include<iostream> using namespace std; int X[] = { 0,0,1
阅读全文
摘要:DFS解决什么问题 给定一个序列,枚举这个序列的所有子序列(可以不连续) 枚举从N个整数中选择K个数的所有方案 背包问题 int n,maxC,V; int W[], C[]; void DFS(int index, int sumW, int sumC) { if (index == n) { r
阅读全文
摘要:输出3的全排列 //输出n的全排列 #include<algorithm> #include<iostream> using namespace std; int main() { int a[] = { 1,2,3 }; do { for (int i = 0; i < 3; i++) { pri
阅读全文
摘要:#include<queue> #include<string> #include<iostream> using namespace std; struct Fruit { int price; string name; }f1,f2,f3; struct cmp { bool operator(
阅读全文
摘要://求n!中有多少个质因子p //时间复杂度O(nlogn) int cal1(int n, int p) { int ans = 0; for (int i = 2; i <= n; i++) { int temp = i; while (temp % p == 0) { ans++; temp
阅读全文
摘要://计算组合数 long long C(long long n, long long m) { if (m == 0 || m == n)return 1; return C(n - 1, m) + C(n-1, m-1); } long long res[67][67] = { 0 }; //递归
阅读全文
摘要:#include<string.h> #include<iostream> struct bign { int d[1000]; int len; bign() { memset(d, 0, sizeof(d)); len = 0; } bign change(char str[]) { bign
阅读全文
摘要:#include<string.h> void *memset(void *buffer, int c, int count) 说明: buffer:为指针或是数组, c:是赋给buffer的值, count:是buffer的长度.
阅读全文
摘要:#include<iostream> #include<math.h> using namespace std; //所谓质因子分解:将一个正整数n分解为一个或多个质数乘积的形式 const int maxn = 100001; int prime[maxn], pNum = 0;//prime表示
阅读全文
摘要:const int maxn = 10000001; int prime[maxn], pNum;//prime存放素数,pNum存放个数 bool p[maxn] = { 0 };//存放i是不是素数 void Find_Prime() { for (int i = 2; i < maxn; i+
阅读全文
摘要:生成N以内随机数 //生成N以内随机数 #include <iostream> #include <cstdlib> using namespace std; int main() { for (int i = 0; i < 10; i++) { cout << rand()%N<< " "; }
阅读全文
摘要:问题描述 正整数a与正整数b所有公共公约数中最大的公约数 代码 int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); }
阅读全文
摘要:问题描述 给定一个递增的正整数序列和一个正整数M,求序列中的两个不同位置的a和b,使它们的和恰好为M,输出所有满足条件的方案。 代码 //给定一个递增的正整数序列和一个正整数M,求序列中的两个不同位置的a和b, //使它们的和恰好为M,输出所有满足条件的方案。 int nums[] = { 1,2,
阅读全文
摘要:问题描述 给定三个正整数a、b、m(a<109,b<106,1<m<109)求ab%m. ##方法一 循环 时间复杂度O(b) 代码 typedef long long LL; LL f(LL a, LL b, LL m) { LL ans = 1; for (int i = 0; i < b; i
阅读全文
摘要:我的写法 //求出序列中第一个大于等于x的元素的位置L int getL(int A[], int left,int right,int X) { //因为位置L等价于不存在X时X在A中的位置,所以初始left=0 right=n //比如说X比当前数组中所有的数都大,则X的位置应为n while
阅读全文

浙公网安备 33010602011771号