原创 動態規劃思想實現KMP算法

#include <iostream> using namespace std; #define MAX 100 //最長子串 int dp[MAX][256] = { 0 }; //dp[狀態][字符]=下一個狀態 void n

原创 洛谷 P1145 約瑟夫

傳送門 #include <iostream> using namespace std; int main() { int k, killed; cin >> k; int m = k; while (1) { m+

原创 一題解決最大公約數與最小公倍數問題

Description 輸入兩個正整數,求它們的最小公倍數。 Input 第一行一個正整數,表示數據組數。 接下來行,每行兩個正整數,表示 Output 每個測試樣例打印一個編號,之後輸出一個正整數,表示答案,保證答案的大小不會超

原创 PTA 簡單動態規劃 樓層仍雞蛋問題

M個軟硬程度一樣但軟硬程度未知的雞蛋,它們有可能都在第一層樓就摔碎,也可能從一千層樓摔下來沒事。有座N層的建築,要你用這M個雞蛋確定哪一層是雞蛋可以安全落下的最高層數。可以摔碎這M個雞蛋,但碎了就不能再拿來測試了,沒碎的可以重複仍

原创 位運算實現排列組合算法

排列數 #include <iostream> #include <vector> using namespace std; int n; int* a = new int[n];//實際中不要用動態數組 vector<int>

原创 STL函數

cctype頭文件中的函數 isalnum(c) //當c是字母或數字時爲真 isalpha(c) //當c是字母時爲真 iscntrl(c) //當c是控制字符時爲真 isdigit(c) //當c是數字時爲真 isgr

原创 快速冪模板

#include <iostream> using namespace std; int main() { int a, b, p; cin >> a >> b >> p; int ans = 1 % p; while (

原创 洛谷 P1962 斐波那契數列

傳送門 #include <iostream> using namespace std; #define ll long long #define MOD 1000000007 struct matrix { ll a1, a

原创 藍橋杯 剪郵票

傳送門 #include <iostream> #include <algorithm> using namespace std; int a[12] = { 0,0,0,0,0,0,0,1,1,1,1,1 };//枚舉剪的五張郵

原创 位運算不完全總結

1.判斷奇偶數 a&1=0; //偶數 a&1=1; //奇數 2.求平均值 (a&b)+((a^b)>>1); //a和b的平均數,結果爲整型數 3.判斷是否爲2的冪 ((x&(x-1))==0)&&(x!=0); //

原创 約瑟夫問題鏈表實現

#include <iostream> using namespace std; struct Node { int data; Node* next; Node(int _data, Node* _next) :data(

原创 C++模板

1.動態開闢二維數組 int n, m;//動態開闢n行m列的二維數組 cin >> n >> m; int** p = new int*[n];//開闢n行 for (int i = 0; i < n; i++)

原创 排列組合算法實現

排列數: #include <iostream> using namespace std; int n, k; int a[25] = { 0 }; int b[25] = { 0 }; int vis[25] = { 0 };

原创 洛谷 P2347 砝碼稱重

傳送門 #include <iostream> #include <algorithm> using namespace std; #define MAX 1000+10 int dp[MAX] = { 0 };//dp[x]表示

原创 揹包問題

01揹包: #include <iostream> #include <algorithm> using namespace std; #define MAX 1000+10 int main() { //變量和數組: int