#ifndef HEADER_AND
#define HEADER_AND
#include
const int A_Max=70;
class And
{//求和为值的组合
private:
int x;
int unit[A_Max];
int dsp[A_Max+1]; //为方便起见,使数值与下标直接对应,浪费一个0好喽
long and(int x, int base, int length);
long Aand_kinds(int x, int base);
long dynamic_state_programming(int x, int base);
public:
And(const int xx=1);
void Aget(int xx);
long Aand();
long Aand_kinds();
long dynamic_state_programming();
};
#endif
/////////////////////////////////////////////
#include "And.h"
And::And(const int xx): x(xx)
{
while (x<0 || x>A_Max)
{
cout<<"请输入大于0,小于"< cin>>x;
}
for (int i=0; i<=A_Max; i++)
dsp[i]=0;
}
void And::Aget(int xx)
{
while (xx<=0 || xx>A_Max)
{
cout<<"请输入大于0,小于"< cin>>xx;
}
x=xx;
}
long And::Aand()
{
return and(x,1,0);
}
long And::and(int x, int base, int length)
{
long count=0;
if (!x)
{
for (int i=0; i cout< cout< return 1;
}
if ( x < base ) return 0;
for (int j=0; j<=x/base; j++)
{
for(int k=0; k unit[length+k]=base;
//内层循环的这一语句很重要。
//因为输出语句都是共用一个数组的,这一句
//的作用就在于清空和刷新数组。
count+=and(x-j*base, base+1, length+j);
}
return count;
}
long And::Aand_kinds()
{
return Aand_kinds(x,1);
}
long And::Aand_kinds(int x, int base)
{
long count=0;
if ( !x ) return 1;
if (x < base) return 0;
for (int i=0; i<=x/base; i++)
count+=Aand_kinds(x-i*base, base+1);
return count;
}
long And::dynamic_state_programming()
{
return dynamic_state_programming(x,1);
}
long And::dynamic_state_programming(int x, int base)
{
long count=0;
if (dsp[x]) return dsp[x];
if ( !x ) return 1;
if (x < base) return 0;
for (int i=0; i<=x/base; i++)
count+=Aand_kinds(x-i*base, base+1);
return dsp[x]=count;
}