华为机试(16)购物单

华为机试(16)购物单_第1张图片

华为机试(16)购物单_第2张图片

import java.util.*;
//动态规划

public class Main{
    public static void main(String[] args){
			Scanner in = new Scanner(System.in);
			while(in.hasNext()) {
			    int N = in.nextInt();
			    int m = in.nextInt();
			    int[][] vm = new int[m+1][N+1];
			    int[] v = new int[m+1];
			    int[] vp = new int[m+1];
			    int[] q = new int[m+1];
			    for(int i = 1; i <= m; i++) {
			        v[i] = in.nextInt(); //价格
			        vp[i] = v[i] * in.nextInt(); //价值
			        q[i] = in.nextInt(); //是否主件
			    }
			    for(int i = 1; i <= m; i++) {
			        for(int j = 1; j <= N; j++) {
			            if(q[i] == 0) { //主件
			                if(v[i] <= j) {
			                    vm[i][j] = Math.max(vm[i-1][j],vm[i-1][j-v[i]]+vp[i]);
			                }
			            }else { //附件
			                if(v[i] + v[q[i]]<= j) {
			                    vm[i][j] = Math.max(vm[i-1][j],vm[i-1][j-v[i]]+vp[i]);
			                }
			            }
			        }
			    }
			System.out.println(vm[m][N]);
			}//end while
    }//end main
}//end class

你可能感兴趣的:(华为机试100+)