当前位置:编程学习 > C/C++ >>

杭电OJ——1085 Holding Bin-Laden Captive!(母函数解答!)

Holding Bin-Laden Captive!


Problem Description
We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But recently, it is reported that he hides in Hang Zhou of China!
“Oh, God! How terrible! ”

 


Don’t be so afraid, guys. Although he hides in a cave of Hang Zhou, he dares not to go out. Laden is so bored recent years that he fling himself into some math problems, and he said that if anyone can solve his problem, he will give himself up!
Ha-ha! Obviously, Laden is too proud of his intelligence! But, what is his problem?
“Given some Chinese Coins (硬币) (three kinds-- 1, 2, 5), and their number is num_1, num_2 and num_5 respectively, please output the minimum value that you cannot pay with given coins.”
You, super ACMer, should solve the problem easily, and don’t forget to take $25000000 from Bush!
 

Input
Input contains multiple test cases. Each test case contains 3 positive integers num_1, num_2 and num_5 (0<=num_i<=1000). A test case containing 0 0 0 terminates the input and this test case is not to be processed.
 

Output
Output the minimum positive value that one cannot pay with given coins, one line for one case.
 

Sample Input
1 1 3
0 0 0
 

Sample Output
4
 

Author
lcy
 
这道题算是母函数当中比较简单的一道题,只要你能够求出所有的组合个数,就可以解出来,关于母函数的详解, 发代码吧!
//母函数问题,整数的拆分

//以前做过这一类的题,现在回顾一下
//我靠,这道题貌似比以前做的母函数的题目还要简单
//只要求出有多少种组合方案就可以了
//以下的两种方法皆可以做!
/*
#include<iostream>
using namespace std;

int main()
{
 int n,m,j,i,k;
 while(cin>>m>>n>>k &&( m!=0 || n!=0 || k!=0))
 {
  int flag[10001],temp[10001];//用于标志可不可以组合,如果可以组合出i,则flag[i]=1,否则为0
  memset(flag,0,sizeof(flag));//初始化,全部标为0
  memset(temp,0,sizeof(temp));
  for(i=0;i<=m;i++)  flag[i]=1;//先是1
  for(i=0;i<=n*2;i=i+2)//再2
  {
   for(j=0;j<=m;j++)
   temp[i+j]=1;
  }
  for(i=0;i<=m+2*n;i++)
   if(temp[i]==1)
    flag[i]=1;
  memset(temp,0,sizeof(temp));
  for(i=0;i<=k*5;i=i+5)//再5
  {
   for(j=0;j<=m+2*n;j++)
    if(flag[j]==1)
     temp[i+j]=1;
  }
  for(i=0;i<=m+2*n+5*k;i++)
   if(temp[i]==1)
    flag[i]=1;
  for(i=0;i<=2*n+m+5*k;i++)
  {
   if(flag[i]==0)
    break;
  }
  cout<<i<<endl;
 }


 return 0;
}
*/

#include<iostream>
using namespace std;
const int lmax=10000;
int c1[lmax+1],c2[lmax+1];

int main()
{
 int n,m,k,i,j;
 while(cin>>m>>n>>k && (m!=0 || n!=0 || k!=0))
 {
  int sum=m+2*n+5*k;
  for(i=0;i<=sum;i++){c1[i]=0;c2[i]=0;}
  for(i=0;i<=m;i++) c1[i]=1;

   for(i=0;i<=m;i++)
    for(j=0;j<=2*n;j=j+2)
     c2[i+j]+=c1[i];
  for(j=0;j<=sum;j++){c1[j]=c2[j];c2[j]=0;}

  for(i=0;i<=m+2*n;i++)
   for(j=0;j<=k*5;j=j+5)
              c2[j+i]+=c1[i];
  for(j=0;j<=sum;j++){c1[j]=c2[j];c2[j]=0;}


     for(i=0;i<=sum;i++) 
     { 
          if(c1[i]==0)  
          {printf("%d\n",i);break;} 
      } 
     if(i==sum+1) 
       printf("%d\n",i); 
  
 }
 return 0;
}

 

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,