母函数:HDU1085 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

题意就是给你价值 1 2 5的这个三种硬币的数量,然后输出最小的,用这三种硬币数量不能组成的价格

#include<stdio.h>
#include<string.h>
//#include <stdbool.h>
#include <algorithm>
#include<queue>
using  namespace std;
//Holding Bin-Laden Captive! 这还是关于母函数的问题
//先创建一个结构体数组,用来存储1 2 5 和他们对应的num
struct N{
    int v;
    int num;
}n[3];
int main()
{
    n[0].v=1;
    n[1].v=2;
    n[2].v=5;
    int i,j,k;
    //还是一样,创建一个存次数系数的数组和存中间值的数组,40是自己定的大小
    int c[40]={0};
    int temp[40]={0};
    //然后输入对应的num
    while(scanf("%d%d%d",&n[0].num,&n[1].num,&n[2].num)!=EOF&&(n[0].num!=0&&n[1].num!=0&&n[2].num!=0)){
        //对第一项赋初值
        for(i=0;i<=n[0].num;i++){//第一项的x可以等于0 到n[0].num
            c[i]=1;
        }
        //然后开始计算,从第二项开始,然后到第三项
        for(i=1;i<=2;i++){//这里首项是0 所以第二项是1
            for(j=0;j<40;j++){
                for(k=0;k+j<40&&k<=(n[i].v*n[i].num);k+=n[i].v){//对第二项也有要求,最大值不能超过该项的硬币总价值
                    temp[k+j]+=c[j];
                }
            }
            //然后开始赋初值
            for(j=0;j<40;j++){
                c[j]=temp[j];
                temp[j]=0;
            }

        }

        //然后开始判断最小的那个,也就是从1开始,第一个系数为0的值
        for(i=1;i<40;i++){
            if(c[i]==0){
                printf("%d\n",i);
                break;//并跳出循环
            }
            continue;

        }
    }


}

发布了72 篇原创文章 · 获赞 5 · 访问量 2815

猜你喜欢

转载自blog.csdn.net/qq_41115379/article/details/104915766
今日推荐