2016huasacm暑假集训训练五 H - Coins
题目链接:https://vjudge.net/contest/126708#problem/H
题意:A有一大堆的硬币,他觉得太重了,想花掉硬币去坐的士;
的士司机可以不找零,但是的士司机也不会多收零钱。
怎么样才能使 A 花的零钱最多。
多重背包模板题
AC代码:
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.io.InputStreamReader; 5 import java.util.Arrays; 6 import java.util.StringTokenizer; 7 public class Main{ 8 public static void main(String args[]) 9 { 10 int N, M; 11 Scanner sc=new Scanner(System.in); 12 while(sc.hasNext()) 13 { 14 N=sc.nextInt(); 15 M=sc.nextInt(); 16 if(N==0&&M==0) break; 17 int a[]=new int[105]; 18 int c[]=new int [105]; 19 int u[]=new int[100001]; 20 for (int i=0; i< N; i++) a[i]=sc.nextInt(); 21 for (int i=0; i< N; i++) c[i]=sc.nextInt(); 22 int sun=0; 23 boolean s[]=new boolean[100001]; 24 s[0]=true; 25 26 for (int i=0; i< N; i++) 27 { 28 Arrays.fill(u, 0); 29 for (int j=a[i]; j<=M; j++) 30 { 31 if (!s[j] && s[j-a[i]] && u[j-a[i]]< c[i]) 32 { 33 s[j]=true; 34 u[j]=u[j-a[i]]+1; 35 sun++; 36 } 37 } 38 } 39 System.out.println(sun); 40 } 41 42 } 43 } 44 class Scanner { 45 BufferedReader br; 46 StringTokenizer st; 47 48 Scanner(InputStream in) { 49 br = new BufferedReader(new InputStreamReader(in)); 50 eat(""); 51 } 52 53 private void eat(String string) { 54 st = new StringTokenizer(string); 55 } 56 57 String nextLine() { 58 try { 59 return br.readLine(); 60 } catch (IOException e) { 61 return null; 62 } 63 } 64 65 boolean hasNext() { 66 while (!st.hasMoreTokens()) { 67 String s = nextLine(); 68 if (s == null) 69 return false; 70 eat(s); 71 } 72 return true; 73 } 74 75 String next() { 76 hasNext(); 77 return st.nextToken(); 78 } 79 80 int nextInt() { 81 return Integer.parseInt(next()); 82 } 83 84 long nextLong() { 85 return Long.parseLong(next()); 86 } 87 88 double nextDouble() { 89 return Double.parseDouble(next()); 90 } 91 }