USACO SECTION 4.1 Beef McNuggets

Beef McNuggets
Hubert Chen

Farmer Brown's cows are up in arms, having heard that McDonalds is considering the introduction of a new product: Beef McNuggets. The cows are trying to find any possible way to put such a product in a negative light.

One strategy the cows are pursuing is that of `inferior packaging'. ``Look,'' say the cows, ``if you have Beef McNuggets in boxes of 3, 6, and 10, you can not satisfy a customer who wants 1, 2, 4, 5, 7, 8, 11, 14, or 17 McNuggets. Bad packaging: bad product.''

Help the cows. Given N (the number of packaging options, 1 <= N <= 10), and a set of N positive integers (1 <= i <= 256) that represent the number of nuggets in the various packages, output the largest number of nuggets that can not be purchased by buying nuggets in the given sizes. Print 0 if all possible purchases can be made or if there is no bound to the largest number.

The largest impossible number (if it exists) will be no larger than 2,000,000,000.

PROGRAM NAME: nuggets

INPUT FORMAT

Line 1:N, the number of packaging options
Line 2..N+1:The number of nuggets in one kind of box

SAMPLE INPUT (file nuggets.in)

3
3
6
10

OUTPUT FORMAT

The output file should contain a single line containing a single integer that represents the largest number of nuggets that can not be represented or 0 if all possible purchases can be made or if there is no bound to the largest number.

SAMPLE OUTPUT (file nuggets.out)

17
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int N;
int Nuggets[10];
int MinNuggets=256;
int Count[10];
long long int Possible[1000000];
long long int Min;
int NowCount=1;
int flag=0;


int main()
{
	FILE *fin,*fout;
	fin=fopen("nuggets.in","r");
	fout=fopen("nuggets.out","w");
	fscanf(fin,"%d",&N);
	int i,j,temp;
	for(i=0;i<N;i++){
		fscanf(fin,"%d",&Nuggets[i]);
		if(Nuggets[i]<MinNuggets)
			MinNuggets=Nuggets[i];
	}
	memset(Count,0,sizeof(Count));	
	Possible[0]=0;
	while(1){
		Min=1999999999;
		for(i=0;i<N;i++){
			if(Possible[NowCount-1]==Possible[Count[i]]+Nuggets[i])
				Count[i]++;
			if(Possible[Count[i]]+Nuggets[i]<Min){
				Min=Possible[Count[i]]+Nuggets[i];
				temp=i;
			}
		}
		Possible[NowCount++]=Min;
		Count[temp]++;
		/*找到存在符合条件的*/ 
		for(j=1;j<MinNuggets&&NowCount>MinNuggets;j++){
			if(Possible[NowCount-j]!=Possible[NowCount-j-1]+1)
				break;
		}
		if(j==MinNuggets)
			break;
		/*找到不存在符合条件的*/ 
		for(j=0;j<N;j++){
			if(Nuggets[j]%2!=0)
			break;
		}
		if(j==N){
			flag=1;
			break;
		}
	}	
	if(flag)
		fprintf(fout,"0\n");
	else
		fprintf(fout,"%d\n",Possible[NowCount-j]-1);
	return 0;
}


用了做丑数那道类似的方法,丑数那个是乘,这个是加,然后在循环内判断是否存在要求的数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值