母函数 hut1028

描述:输入N,求解有几种和式;

#include"iostream"
using namespace  std;
int c1[130],c2[130];
int main()
{
     int N;
     int i,j,k;
      while(scanf("%d",&N)!=EOF)
      {
          for(i=0;i<=N;++i)
          {
             c1[i]=1;
             c2[i]-0;                
          }
          for(i=2;i<=N;++i)
          {
              for(j=0;j<=N;++j)
                 for(k=0;k+j<=N;k+=i)
                     c2[j+k]+=c1[j];                                        
               for(j=0;j<=N;++j)
               {
                   c1[j]=c2[j];
                   c2[j]=0;    
               }
          }
          printf("%d\n",c1[N]);
      }  
      return 0;
}
或者是:定义一个常数量 _max值不改变

#include"iostream"
using namespace std;
const  int _max=130;
int num1[_max],num2[_max];
int main()
{
   int N,P;
   int i,j,k;
   while(cin>>N)
   {
       for(int i=0;i<=N;++i)
       {
          num1[i]=1;
          num2[i]=0;       
       }
       for(i=2;i<=N;++i)
       {
          for(j=0;j<=N;++j)
          {
              for(k=0;j+k<=N;k+=i)
                num2[j+k]+=num1[j];                
          }                
          for(j=0;j<=N;++j)
          {
             num1[j]=num2[j];
             num2[j]=0;                
          }
       }            
      cout<<num1[N]<<endl;
   }
    return 0;  
}

用memset初始化就会出现错误,太气人了!(可能会超出范围)以下的程序就是错误的:

#include"iostream"
using namespace std;
const  int _max=200;
int num1[_max],num2[_max];
int main()
{
   int N,P;
   int i,j,k;
   while(scanf("%d",N)!=EOF)
   {
       memset(num1,1,sizeof(num1));
       memset(num2,0,sizeof(num2));//越界;
       for(i=2;i<=N;++i)
       {
          for(j=0;j<=N;++j)
          {
              for(k=0;j+k<=N;k+=i)
                num2[j+k]+=num1[j];                
          }                
          for(j=0;j<=N;++j)
          {
             num1[j]=num2[j];
             num2[j]=0;                
          }
       }            
       printf("%d\n",num1[N]);
   }
    return 0;  
}

posted @ 2011-08-04 16:08  bcy  阅读(147)  评论(0编辑  收藏  举报