leetcode动态规划切钢筋
作者:互联网
钢筋的价值和长度有关,以下为初始数据
v[0] = 0;
v[1] = 1;
v[2] = 5;
v[3] = 8;
v[4] = 9;//不是最大值,注意
v[5] = 10;
v[6] = 20;
v[7] = 20;
v[8] = 20;
v[9] = 20;
v[10] = 20;
思路
- 动态规划,只管最后一刀切的位置
- 需要将n之前的长度的价值最大值求取出来,进行累计
#include<stdio.h>
#include <string.h>
#define MAXLEN 1000
#define MAX(a,b) ((a) >(b)? a:b)
void MaxValueC(int n,int *v,int k){
if(n==0 || n==1||n==2|| n==3) {
return ;
} else {
for(int j = 0;j<=n;j++)
for (int i = 0; i<=j&&i<=10; i++) {
v[j] = MAX(v[i]+v[j-i],v[j]);
}
}
}
int MaxValue(int n) {
int v[MAXLEN];
memset(v, 0,sizeof(v));
int k = 0;
v[0] = 0;
v[1] = 1;
v[2] = 5;
v[3] = 8;
v[4] = 9;
v[5] = 10;
v[6] = 20;
v[7] = 20;
v[8] = 20;
v[9] = 20;
v[10] = 20;
MaxValueC(n,v,k);
for (int i=0; i<=n; i++) {
printf("%d ",v[i]);
}
printf("\n");
return v[n];
}
int main () {
int n = 30;
int re = MaxValue(n);
printf("re = %d\n",re);
}
标签:10,20,MaxValueC,int,钢筋,re,printf,动态,leetcode 来源: https://blog.csdn.net/weixin_41672404/article/details/120219092