题目链接
分析:在DFS暴力破解已经非常熟练的情况下是没有任何难度的.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static int count = 0;
public static void main(String[] args){
Scanner input;
try {
input = new Scanner(new File("D:/123.txt"));
ArrayList<Integer> list = new ArrayList<Integer>();
while(input.hasNext()){
int weidth = input.nextInt();
int height = input.nextInt();
if(height<=20){
list.add(weidth);
}
}
input.close();
System.out.println(dfs(list,0,0,0));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int dfs(ArrayList<Integer> list,int index,int x,int n){
if(x>100){
return Integer.MIN_VALUE;
}
if(index==list.size()){
if(x==100){
return n;
}
return Integer.MIN_VALUE;
}
return Math.max(dfs(list,index+1,x+list.get(index),n+1),dfs(list,index+1,x,n));
}
}