首页 > ACM题库 > HDU-杭电 > HDU 4422-The Little Girl who Picks Mushrooms-枚举-[解题报告]HOJ
2015
07-16

HDU 4422-The Little Girl who Picks Mushrooms-枚举-[解题报告]HOJ

The Little Girl who Picks Mushrooms

问题描述 :

It’s yet another festival season in Gensokyo. Little girl Alice planned to pick mushrooms in five mountains. She brought five bags with her and used different bags to collect mushrooms from different mountains. Each bag has a capacity of 2012 grams. Alice has finished picking mushrooms in 0 ≤ n ≤ 5 mountains. In the i-th mountain, she picked 0 ≤ wi ≤ 2012 grams of mushrooms. Now she is moving forward to the remained mountains. After finishing picking mushrooms in all the five mountains, she want to bring as much mushrooms as possible home to cook a delicious soup.
Alice lives in the forest of magic. At the entry of the forest of magic, live three mischievous fairies, Sunny, Lunar and Star. On Alice’s way back home, to enter the forest, she must give them exactly three bags of mushrooms whose total weight must be of integral kilograms. If she cannot do so, she must leave all the five bags and enter the forest with no mushrooms.
Somewhere in the forest of magic near Alice’s house, lives a magician, Marisa. Marisa will steal 1 kilogram of mushrooms repeatedly from Alice until she has no more than 1 kilogram mushrooms in total. So when Alice gets home, what’s the maximum possible amount of mushrooms Alice has? Remember that our common sense doesn’t always hold in Gensokyo. People in Gensokyo believe that 1 kilogram is equal to 1024 grams.

输入:

There are about 8192 test cases. Proceed to the end of file.
The first line of each test case contains an integer 0 ≤ n ≤ 5, the number of mountains where Alice has picked mushrooms. The second line contains n integers 0 ≤ wi ≤ 2012, the amount of mushrooms picked in each mountain.

输出:

There are about 8192 test cases. Proceed to the end of file.
The first line of each test case contains an integer 0 ≤ n ≤ 5, the number of mountains where Alice has picked mushrooms. The second line contains n integers 0 ≤ wi ≤ 2012, the amount of mushrooms picked in each mountain.

样例输入:

1
9
4
512 512 512 512
5
100 200 300 400 500
5
208 308 508 708 1108

样例输出:

1024
1024
0
792
Hint
In the second sample, if Alice doesn't pick any mushrooms from the 5-th mountain. She can give (512+512+0) =1024 grams of mushrooms to Sunny, Lunar and Star. Marisa won't steal any mushrooms from her as she has exactly 1 kilogram of mushrooms in total. In the third sample, there are no three bags whose total weight is of integral kilograms. So Alice must leave all the five bags and enter the forest with no mushrooms. In the last sample: 1.Giving Sunny, Lunar and Star: (208+308+508)=1024 2.Stolen by Marisa: ((708+1108)-1024)=792

/*
分析:
    枚举五种情况分析就行了。
    
                                2012-10-22
*/

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
int main()
{
	int n,num[11];
	int i,l,j,k;
	int flag;
	int temp;
	int ans;
	while(scanf("%d",&n)!=-1)
	{
		for(i=0;i<n;i++)	scanf("%d",&num[i]);
		if(n<=3)	{printf("1024\n");continue;}
		if(n==4)
		{
			flag=0;
			for(i=0;i<4;i++)
			{
				temp=0;
				for(l=0;l<4;l++)
				{
					if(i==l)	continue;
					temp+=num[l];
				}
				if(temp%1024==0)	flag=1;
				if(flag)	break;
			}
			if(flag)	{printf("1024\n");continue;}
			else
			{
				ans=0;
				for(i=0;i<4;i++)
				for(l=0;l<4;l++)
				{
					if(i==l)	continue;
					temp=num[i]+num[l];
					while(temp>1024)	temp-=1024;
					ans=ans>temp?ans:temp;
				}
				printf("%d\n",ans);
				continue;
			}
		}
		else
		{
			flag=0;
			ans=0;
			for(i=0;i<5;i++)
			for(l=0;l<5;l++)
			for(j=0;j<5;j++)
			{
				if(i!=l && i!=j && l!=j)
				{
					temp=num[i]+num[l]+num[j];
					if(temp%1024==0)
					{
						temp=0;
						for(k=0;k<5;k++)	if(k!=i && k!=l && k!=j)	temp+=num[k];
						while(temp>1024)	temp-=1024;
						ans=ans>temp?ans:temp;
						flag=1;
					}
				}
			}
			if(flag)	printf("%d\n",ans);
			else		printf("0\n");
		}
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/ice_crazy/article/details/8100131