Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Java

Posted by 201722362014068 at 2019-03-24 00:36:03 on Problem 1837
import java.util.Scanner;


public class Main {

	//砝码问题
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext()) {
		
		int C=in.nextInt();
		int G=in.nextInt();
		if(C<2||C>20||G<2||G>20) {
			return;
		}
		//注意 在运用nextLine()读数据之前,要调用一下nextLine()方法
		in.nextLine();
		int c[]=new int[C+1];
		int w[] = new int[G+1];
		int[][] dp = new int[25][16000];
		String s1 = in.nextLine();
		String[] sb1 = s1.split(" ");
		for(int i=1;i<=C;i++) {
			c[i] = Integer.parseInt(sb1[i-1]);
		}
		
		String s2 = in.nextLine();
		String[] sb2 = s2.split(" ");
		for(int j=1;j<=G;j++) {
			w[j]=Integer.parseInt(sb2[j-1]);
		}
		
		dp[0][7500]=1;
		
		for(int i=1;i<=G;i++) {
			for(int j=1;j<=15000;j++) {
				for(int k=1;k<=C;k++) {
					if(j>=c[k]*w[i]) {
						dp[i][j]=dp[i][j]+dp[i-1][j-c[k]*w[i]];
					}
				}
			}
		}
		System.out.println(dp[G][7500]);
		}
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator