標籤:wap ++i stdout i+1 stdio.h pen highlight algorithm nbsp
最大子序列和
dp轉移方程 sum[i] = max{sum[i-1]+a[i],a[i]}
int MaxSum(int n){ int sum=0,tmp=0; for(int i=0;i<n;i++) { if(tmp>0) tmp+=a[i]; else tmp=a[i]; if(tmp>sum) sum=tmp; } return sum;}
遞迴求全排列
先處理第一層,比如123 第一位置分別和第一個位置、第二個位置、第三個位置交換 ->{123 213 321}
再遞迴處理第二層 比如 123 第二位置分別和第二位置、第三位置交換 ->{ 123 132}
再遞迴處理第三層 比如 123 第三位置和第三位置交換 ->{123}
然後就是回溯分別處理
#include <stdio.h>#include <algorithm>using namespace std;int ans = 0;void permutation(int k, int n, int a[]){ if(k == n-1){ ans+=1; for(int i = 0; i < n; ++i) printf("%d ", a[i]); puts(""); }else{ for(int i = k; i < n; ++i){ swap(a[k],a[i]); permutation(k+1, n, a); swap(a[k],a[i]); } }}int main(){ freopen("1.txt","w",stdout); int a[100]; int n = 3; for(int i = 0; i < n; ++i) a[i] = i+1; permutation(0, n, a); printf("%d\n",ans); return 0;}
多做點記錄,只有好處,沒壞處。認真一點,進個大公司!
小演算法總結