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
还是怪自己太年轻啊,wr了这么多遍,一直以为代码有毛病,最后只是发现没处理越界的情况,无语呀
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n[4];
int c[4]={1,2,5};
int a[8005],b[8005];
int main()
{
while (~scanf("%d%d%d",&n[0],&n[1],&n[2]),n[0]+n[1]+n[2])
{
int i,j,k,sum;
sum=c[0]*n[0]+c[1]*n[1]+c[2]*n[2];
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for (i=0;i<=n[0];i++)
a[i]=1;
for (i=1;i<3;i++)
{
for (j=0;j<=sum;j++)
for (k=0;k<=n[i]&&j+k*c[i]<=sum;k++)
b[j+k*c[i]]+=a[j];
for (j=0;j<=sum;j++)
{
a[j]=b[j];
b[j]=0;
}
}
int flag=0;
for (i=1;i<=sum;i++)
if (!a[i])
{
flag=1;
printf("%d\n",i);
break;
}
if (flag==0)
printf("%d\n",sum+1);
}
return 0;
}
另一种解法就是发现规律,如果硬币1的数量没有的话,那么结果肯定是1,如果硬币1跟2的总价值还没能超过4,只要硬币1跟2存在的话,就能组成1到3之间的数字,那么结果就会是它们两个的总价值+1,最后一种情况就是在三种钱币所能表示的总范围里,都能表示出来,那么只能是最终的总价值+1喽
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a,b,c;
while (~scanf("%d%d%d",&a,&b,&c),a+b+c)
{
if (a==0)
printf("1\n");
else if (a+2*b<4)
printf("%d\n",a+2*b+1);
else
printf("%d\n",a+2*b+5*c+1);
}
return 0;
}