Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Javaimport 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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator