小演算法總結

來源:互聯網
上載者:User

標籤: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;}

  

 多做點記錄,只有好處,沒壞處。認真一點,進個大公司!

小演算法總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.