日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

动态规划解题套路框架

發布時間:2024/10/8 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 动态规划解题套路框架 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

動態規劃解題套路框架

另外!!!# define maxn 100005最好多5個

509.斐波那契數
322.零錢兌換

斐波那契數

#include <iostream> #include<bits/stdc++.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; #define maxn 100000 int mem[maxn]={0};int dp(int n) {if(n==1||n==2){mem[n]=1;return 1;}if(mem[n]!=0){return mem[n];}mem[n]=dp(n-1)+dp(n-2);///確實好久不寫了,這里一定要用函數 return mem[n];}int main(int argc, char** argv) {cout<<dp(8);return 0; }

零錢兌換

#include <iostream> #include<bits/stdc++.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; #define maxn 10000 int coin[3]={1,2,5}; int coinnum=3; ///int mem[maxn]={maxn};這句話是錯的,初始不賦0的話只能賦第一個 int mem[maxn]={0}; int dp(int n)///還剩n元 {if(n==0)///小于零的一會在選擇上踢出去 {return 0;}if(mem[n]!=0){return mem[n];}int res=maxn;for(int i=0;i<coinnum;i++){if(n-coin[i]<0){continue;}res=min(res,dp(n-coin[i])+1);}mem[n]=res;return mem[n];}int main(int argc, char** argv) {cout<<dp(11);return 0; }

錯誤代碼:::!!!!!!

#include <iostream> #include<bits/stdc++.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; #define maxn 10005 int coin[3]={1,2,5}; int coinnum=3; ///int mem[maxn]={maxn};這句話是錯的,初始只能賦0 int mem[maxn]={0}; int dp(int n)///還剩n元 {if(n==0)///小于零的一會在選擇上踢出去 {return 0;}if(mem[n]!=maxn){return mem[n];}for(int i=0;i<coinnum;i++){if(n-coin[i]<0){continue;}mem[n]=min(mem[n],dp(n-coin[i])+1);}return mem[n];}int main(int argc, char** argv) {for(int i=0;i<=11;i++)//這里寫錯成了沒有等號,結果一調mem【11】就不相同,為0{mem[i]=maxn;}cout<<dp(11);return 0; }

總結

以上是生活随笔為你收集整理的动态规划解题套路框架的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。