给定一个整数N表示线段的长度。您需要以线段每次切割长度为x,y或z的整数的方式剪切线段。并且在执行所有切割操作之后,切割段的总数量必须是最大的。分割错误
#include <iostream>
#include<vector>
#include<math.h>
#include<climits>
#include<algorithm>
using namespace std;
int count(int*dp,int x,int y,int z,int N)
{
if(N==0)
{
return 1;
}
if(N<0)
{
return 0;
}
if(dp[N]!=INT_MAX)
{
return dp[N];
}
dp[N]=max(count(dp,x,y,z,x)+count(dp,x,y,z,N-x)
,max(count(dp,x,y,z,y)+count(dp,x,y,z,N-y),
count(dp,x,y,z,z)+count(dp,x,y,z,N-z)));
return dp[N];
}
int main()
{
//code
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n;
cin>>n;
int x,y,z;
cin>>x;
cin>>y;
cin>>z;
//cout<<x<<y<<z<<n;
int*dp=(int*)malloc(sizeof(int)*n+1);
// int dp[n+1];
dp[0]=1;
for(int i=1;i<=n;i++)
{
dp[i]=INT_MAX;
}
cout<<x<<y<<z<<n;
cout<<count(dp,x,y,z,n)<<endl;
}
return 0;
}
[调试你的程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。不要倾倒在我们身上。 – StoryTeller
首先,不要在C++中使用'malloc',而应使用'new []'。然后学会在这种情况下根本不使用动态分配,而是使用['std :: vector'](http://en.cppreference.com/w/cpp/container/vector)。最后,虽然您可能会分配足够的空间来使用基于1的数组(或向量)索引,但请避免这种情况,并且可能会让所有读取代码的人感到困惑。 –
至于你的问题,你有没有检查你的递归不深?您是否因*全部*输入而崩溃?还是只为一些输入? –