题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4422
1 9 4 512 512 512 512 5 100 200 300 400 500 5 208 308 508 708 1108
1024 1024 0 792HintIn 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
题意:
一共有5座山,每座山有很多的蘑菇,给出一个 n 表示已经采了 n 座山上的蘑菇,求最多带回去多少蘑菇。。
当采完5座山后,有三个人拿走三个袋子里的蘑菇,这三个的袋子的蘑菇数量为1024的倍数;
如果没有三个袋子的蘑菇数目满足情况,则拿走5个袋子,即不能带走任何蘑菇。
并且回家的路上有人偷蘑菇,一直偷,直到总蘑菇数不大于1024;
PS:如果n小于等于3,那么一定能拿回家1024的蘑菇!
如果n大于3,则分类讨论!
代码如下:
#include <cstdio> #include <cstring> int main() { int a[7]; int t; int n; while(~scanf("%d",&n)) { int sum = 0; for(int i = 0; i < n; i++) { scanf("%d",&a[i]); sum+=a[i]; } if(n <= 3) { printf("1024\n"); continue; } int maxx = 0, tt = 0; if(n == 4) { int flag = 0; for(int i = 0; i < 4; i++) { for(int j = i+1; j < 4; j++) { for(int k = j+1; k < 4; k++) { if((a[i]+a[j]+a[k])%1024 == 0) { flag = 1; } } } } if(flag) { printf("1024\n"); continue; } for(int i = 0; i < 4; i++) { for(int j = i+1; j < 4; j++) { tt= a[i]+a[j]; while(tt > 1024) tt-=1024; if(tt > maxx) { maxx = tt; } } } printf("%d\n",maxx); continue; } if(n == 5) { for(int i = 0; i < 5; i++) { for(int j = i+1; j < 5; j++) { for(int k = j+1; k < 5; k++) { int ss = a[i]+a[j]+a[k]; if(ss%1024==0) { tt = sum - ss; while(tt >1024) tt-=1024; if(tt > maxx) { maxx = tt; } } } } } printf("%d\n",maxx); } } return 0; }
HDU 4422 The Little Girl who Picks Mushrooms(数学)
原文地址:http://blog.csdn.net/u012860063/article/details/40918029