HDU1712 ACboy needs your help

HDU 水题 背包问题 分组背包问题 注意 这个

1             for (int j=1;j<=M;j++)
2                 scanf("%d",&A[j]);

不能写成这个

            for (int j=0;j<M;j++)
                scanf("%d",&A[j]);

题目及AC代码 如下

ACboy needs your help

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3626    Accepted Submission(s): 1880


Problem Description
ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he will gain from different course depending on the days he spend on it.How to arrange the M days for the N courses to maximize the profit?
 
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers N and M, N is the number of courses, M is the days ACboy has.
Next follow a matrix A[i][j], (1<=i<=N<=100,1<=j<=M<=100).A[i][j] indicates if ACboy spend j days on ith course he will get profit of value A[i][j].
N = 0 and M = 0 ends the input.
 
Output
For each data set, your program should output a line which contains the number of the max profit ACboy will gain.
 
Sample Input
2 2
1 2
1 3
2 2
2 1
2 1
2 3
3 2
1 3
2 1
0 0
 
Sample Output
3
4
6
 
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 
 5 const int max_n=110;
 6 
 7 int max(int a,int b)
 8 {
 9     return a>b?a:b;
10 }
11 
12 int main(void)
13 {
14     int A[max_n];
15     int dp[max_n];
16     int M,N;
17     while(scanf("%d%d",&N,&M)&& (N!=0 && M!=0))
18     {
19         memset(dp,0,sizeof(dp));
20         memset(A,0,sizeof(A));
21         for(int i=0;i<N;i++)
22         {
23             for (int j=1;j<=M;j++)
24                 scanf("%d",&A[j]);
25 
26             for(int vi=M;vi>=0;vi--)
27             {
28                 for(int k=0;k<=vi;k++)
29                     dp[vi]=max(dp[vi],dp[vi-k]+A[k]);
30             }
31         }
32         printf("%d
",dp[M]);
33     }
34 
35 
36     return 0;
37 }
 
原文地址:https://www.cnblogs.com/VOID-133/p/3639127.html