HDU 4422 The Little Girl who Picks Mushrooms(简单题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4422

题目大意:小姑娘背着5个包去山上采蘑菇,每座山上只能用一个背包采集。三个小精灵会要她3个背包,其里面蘑菇的数量必须是1024的倍数,否则5个背包都会被拿走。然后魔术师会从她剩下的背包里取走蘑菇,每次取1024克,直到蘑菇不超过1024克为止。求小姑娘最多能得到多少蘑菇。

Sample Input
1
9
4
512 512 512 512
5
100 200 300 400 500
5
208 308 508 708 1108
 
Sample Output
1024
1024
0
792

代码如下:

 1 # include<stdio.h>
 2 # include<algorithm>
 3 # include<iostream>
 4 using namespace std;
 5 
 6 int n,a[6],sum,ans;
 7 
 8 int calc(int x)
 9 {
10     while(x > 1024)
11         x -= 1024;
12     return x;
13 }
14 
15 void solve()
16 {
17     for(int i=0; i<n; i++)
18         for(int j=i+1; j<n; j++)
19             if(n==4  || (sum-a[i]-a[j])%1024==0)
20                 ans = max(ans,calc(a[i]+a[j]));
21 }
22 
23 int main()
24 {
25     while(scanf("%d",&n)!=EOF)
26     {
27         sum = 0,ans = 0;
28         for(int i=0; i<n; i++)
29         {
30             scanf("%d",&a[i]);
31             sum += a[i];
32         }
33         if(n<=3)
34             ans = 1024;
35         else if(n==4)
36             for(int i=0; i<4; i++)
37                 if((sum-a[i])%1024==0)
38                     ans = 1024;
39         if(ans!=1024)
40             solve();
41         printf("%d
",ans);
42     }
43     return 0;
44 }
原文地址:https://www.cnblogs.com/acm-bingzi/p/3355336.html