数论-北大-1014

0 views
Skip to first unread message

china_sjc

unread,
Aug 6, 2008, 5:34:22 AM8/6/08
to 中国矿业大学徐海学院算法课程
#include <iostream.h>

int marble[6];
const int limit[]={3,4,5,4,6,5};

bool half(int d,int next)
{
if(d==0)
return true;
if(next==0)
return false;
int i;
for(i=marble[next-1];i>=0;i--)
{
if(d>=i*next)
{
marble[next-1]-=i;
if(half(d-i*next,next-1))
return true;
marble[next-1]+=i;
}
}
return false;
}

int main()
{
int i,test=1,sum=0;
for(i=0;i<6;i++)
{
cin>>marble[i];
if(marble[i]>limit[i])
{
marble[i]=limit[i]-(marble[i]-limit[i])%2;
}
sum+=marble[i]*(i+1);
}
while(sum!=0)
{
if(sum%2==0&&half(sum/2,6))
{
cout<<"Collection #"<<test++<<":"<<endl;
cout<<"Can be divided.\n"<<endl;
}
else
{
cout<<"Collection #"<<test++<<":"<<endl;
cout<<"Can't be divided.\n"<<endl;
}
sum=0;
for(i=0;i<6;i++)
{
cin>>marble[i];
if(marble[i]>limit[i])
{
marble[i]=limit[i]-(marble[i]-limit[i])%2;
}
sum+=marble[i]*(i+1);
}
}
return 0;
}
Reply all
Reply to author
Forward
0 new messages