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!
“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
题目大意:有三种硬币,它们的面值分别是1、2、5,输入三个数num1、num2、num5表示三种硬币的个数,请输出这些硬币不能组成的最小的数
解题思想:还是要用母函数,不过和母函数模板还是不一样,需要自己改动一下。
AC代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int c1[8010],c2[8010];
int main()
{
int i,j,k,num1,num2,num5,sum;
while(scanf("%d%d%d",&num1,&num2,&num5)&&(num1!=0||num2!=0||num5!=0))
{
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
sum=1*num1+2*num2+5*num5;
for(i=0;i<=sum;i++) //为第一项多项式赋值
{
c1[i]=1;
c2[i]=0;
}
for(i=0;i<=num1;i++) //将num1个面值为1的硬币和num2个面值为2的硬币进行组合
{
for(j=0;j<=num2*2;j=j+2)
{
c2[i+j]=c2[i+j]+c1[i];
}
}
for(i=0;i<=num1+2*num2;i++) //将这两种硬币组合的结果存放在数组c1种
{
c1[i]=c2[i];
c2[i]=0;
}
for(i=0;i<=num1+2*num2;i++) //将前两种组合得到的结果和面值为5的硬币进行组合
{
for(j=0;j<=num5*5;j=j+5)
{
c2[i+j]=c2[i+j]+c1[i];
}
}
for(i=0;i<=num1+2*num2+5*num5;i++)
{
c1[i]=c2[i];
c2[i]=0;
}
for(i=0;i<=sum;i++) //c1数组中存放的是多项式的系数,所以当c1[i]==0时表示无法组合成这个数,将它输出即可
{
if(c1[i]==0)
{
printf("%d\n",i);
break;
}
}
if(i==sum+1) //这种情况是指1—sum都可以组合,因为那些硬币最大才可以组合成sum,所以sum+1就是那个最小的无法组合的数
printf("%d\n",i);
}
return 0;
}
#include<string.h>
#include<stdlib.h>
int c1[8010],c2[8010];
int main()
{
int i,j,k,num1,num2,num5,sum;
while(scanf("%d%d%d",&num1,&num2,&num5)&&(num1!=0||num2!=0||num5!=0))
{
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
sum=1*num1+2*num2+5*num5;
for(i=0;i<=sum;i++) //为第一项多项式赋值
{
c1[i]=1;
c2[i]=0;
}
for(i=0;i<=num1;i++) //将num1个面值为1的硬币和num2个面值为2的硬币进行组合
{
for(j=0;j<=num2*2;j=j+2)
{
c2[i+j]=c2[i+j]+c1[i];
}
}
for(i=0;i<=num1+2*num2;i++) //将这两种硬币组合的结果存放在数组c1种
{
c1[i]=c2[i];
c2[i]=0;
}
for(i=0;i<=num1+2*num2;i++) //将前两种组合得到的结果和面值为5的硬币进行组合
{
for(j=0;j<=num5*5;j=j+5)
{
c2[i+j]=c2[i+j]+c1[i];
}
}
for(i=0;i<=num1+2*num2+5*num5;i++)
{
c1[i]=c2[i];
c2[i]=0;
}
for(i=0;i<=sum;i++) //c1数组中存放的是多项式的系数,所以当c1[i]==0时表示无法组合成这个数,将它输出即可
{
if(c1[i]==0)
{
printf("%d\n",i);
break;
}
}
if(i==sum+1) //这种情况是指1—sum都可以组合,因为那些硬币最大才可以组合成sum,所以sum+1就是那个最小的无法组合的数
printf("%d\n",i);
}
return 0;
}
解题体会:忽然感觉对母函数理解了一些