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.母函数求解,只要接触过母函数的人应该可以轻松解决
代码:
#include<stdio.h> #include<string.h> #define max 100000 //本题测试数据与题目不符,应该偏大 int c1[max+1];//可以组成的结果存储在c1 int c2[max+1];//中间结果 int num[3]={1,2,5};//面值 int num_i[3];//每种面值的数量 int main() { int max_number,i,j,k; // max_number为所有面值加起来可以达到的最大值 while(scanf("%d%d%d",&num_i[0],&num_i[1],&num_i[2])) { if(!num_i[0]&&!num_i[1]&&!num_i[2]) break;//退出条件 max_number=num_i[0]+num_i[1]*num[1]+num_i[2]*num[2]; memset(c1,0,sizeof(c1)); memset(c2,0,sizeof(c2)); c1[0]=1; //母函数 for(i=0;i<3;i++) { for(j=0;j<=num_i[i];j++) { for(k=0;k+j*num[i]<=max_number;k++) { c2[k+j*num[i]]+=c1[k]; } } for(j=0;j<=max_number;j++) { c1[j]=c2[j]; c2[j]=0; } } //此处是max_number+1而不是max_number for(i=0;i<=max_number+1;i++) { if(!c1[i]) { printf("%d\n",i); break; } } } return 0; }
①如果可以组成1-4中间的任何数字,那么最小不可以组成的数值为所有面值加起来+1;
②如果不可以,则应该是1面值和2面值加起来+1
代码:
#include <stdio.h> int main() { int o,t,f,min; while(1) { scanf("%d %d %d",&o,&t,&f); if(!o&&!t&&!f) break; if(!o) min=1; else { if(o+2*t<4) min=(o+2*t)+1; else min=o+2*t+5*f+1; } printf("%d\n",min); } return 0; }