本文主要是介绍HDU——1085 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
解题思路:
利用母函数的普通母函数解决问题。
母函数讲解链接:https://blog.csdn.net/vsooda/article/details/7975485
代码:
注:G++ AC ,C++ WA
#include <stdio.h>
#include <string.h>
using namespace std;int ways[8001]; //记录 获取当前价值(下标值) 可以有几种组合方法
int temp[8001]; //ways 的 中间值。
int coin[4] = {0 , 1 , 2 ,5};
int numcoin[4];
int main(){int curmax = 0 , i ,j ,k; //当前拥有的所有硬币可以得到的最大价值while(scanf("%d %d %d",&numcoin[1] , &numcoin[2] , &numcoin[3]) == 3){if(numcoin[1] == 0 && numcoin[2] == 0 && numcoin[3] == 0)break;curmax = 0;memset(ways , 0 , sizeof(ways));memset(temp , 0 , sizeof(temp));curmax = coin[1] * numcoin[1]; //只有第一种硬币时可以获得的最大价值for(i = 0 ; i <= curmax ; i ++)ways[i] = 1; //因为 第一种硬币的价值是 1 ,所以直至他能所表示的最大价值 ,每种价值都可以表示出来 ,且都只有一种表示方式。for(i = 2 ; i <= 3 ; i++){curmax = curmax + coin[i] * numcoin[i] ; //加入第i中硬币,能获得的总价值也随之变大。for( k = 0 ; k <= curmax - coin[i] * numcoin[i]; k ++){for(j = 0 ; j <= curmax ; j = j + coin[i]){temp[k + j] = temp[j + k] + ways[k] ;//加上第 i 中硬币 , 获得价值(k + j)的方法数 = 获得价值 j 的方法数 * 获得价值 k 的方法数。//其中价值 j 由第 i 中硬币组成(只有一种方法)//价值 k 的组成方法为 ways[k]}}for(k = 0 ; k <= curmax ; k++)ways[k] = temp[k];memset(temp , 0 , sizeof(temp));}for(i = 0 ; i <= curmax ; i++)if(ways[i] == 0){ //表示有 0 中组合方法可以获得价值 i。即无法获得价值 i.printf("%d\n",i);break;}if(i == curmax + 1) //最大价值以内的值都能够表示,则结果输出为 最大价值+1printf("%d\n",i);}return 0;
}
这篇关于HDU——1085 Holding Bin-Laden Captive!(母函数)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!