原创 關於求餘問題

以a%b爲例,求得的餘數的符號與a的保持一致,因此求模運算符求得的餘數有存在負數的可能性。 r=a%b有a=k*b+r 使r1=(r+b)%b=(r%b+b%b)%b=r%b%b=r%b=r,注意但是此時由於r+b,所以r若爲負數

原创 Floyd算法計算最短路徑和迪傑斯特拉算法

#include<stdio.h> int ans[101][101]; int main() { int n, m; while (scanf("%d%d", &n, &m) != EOF) { if (m == 0

原创 最長遞增子序列

#include<stdio.h> int max(int a, int b) { return a > b ? a : b; } int list[26]; int dp[26]; int main() { int n;

原创 最長公共子序列

#include<stdio.h> #include<string> using namespace std; int max(int a, int b) { return a > b ? a : b; } int dp[101

原创 N封信封全部裝錯(錯排公式)

錯排公式:F[n]=(n-1)*F[n-1]+(n-2)*F[n-2] #include<stdio.h> long long F[21]; int main() { F[1] = 0; F[2] = 1; for (int

原创 N階樓梯上樓問題(遞推求解)

N階樓梯上樓問題:一次可以走一階或者兩階,問有多少種上樓方式。 因爲只可能從n-1階和n-2階走到n階,因此走到n階的方式數量=走到n-1階的方式數量+走到n-2階的方式的數量。 也就是F[n]=F[n-1]+F[n-2],也就是

原创 求哈夫曼樹帶權路徑長度和

#include<stdio.h> #include<queue> using namespace std; priority_queue<int, vector<int> ,greater<int>> q; int main()

原创 二叉樹的遍歷,建樹,還原

#include<stdio.h> #include<string> using namespace std; struct node { node *lchild; node *rchild; char c; }tree[

原创 分解素因數

設x=p1^e1 +p2^e2… 求x的p1…pn和e1…en #include<stdio.h> int prime[10000]; bool mark[10001]; int size1; void init() { for

原创 二分法查找學生信息

#include<algorithm> #include<iostream> #include<stdio.h> #include<string.h> using namespace std; struct student {

原创 遞歸應用二:Flood Fill算法,將相鄰的點聯合成一個塊

#include<stdio.h> char maze[100][100]; bool mark[100][100]; int n, m; int find[][2] = { 1,0, 0,1, -1,0, 0,-1,