package huawei;
import java.util.ArrayList;
import java.util.Scanner;
/**
* 和尚挑水
*/
public class FindWater {
private static final String inCase = "1 0 0 1 0 1 0\n" +
"0 0 1 1 0 0 0\n" +
"0 1 0 0 1 0 1\n" +
"1 1 0 0 0 1 0\n" +
"0 0 1 0 1 0 0\n" +
"0 0 0 1 0 0 1\n" +
"1 0 0 0 1 0 0";
private static int input[][] = new int[7][7];
private static ArrayList<Integer> list = new ArrayList<Integer>();
private static ArrayList<ArrayList<Integer>> allList = new ArrayList<>();
//visited[i]的值描述地i个和尚是否被访问过,1表示已被访问,0表示未被访问;
private static boolean visited[] = new boolean[7];
public static void main(String[] args) {
Scanner in = new Scanner(inCase);
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
input[i][j] = in.nextInt();
}
}
backTrack();
//输出方案
System.out.println(allList.size());
for (ArrayList<Integer> list : allList) {
System.out.println(list);
}
}
//用递归来实现回溯,t代表第t天
private static void backTrack() {
if (list.size() == 7) {
allList.add(new ArrayList<>(list));
return;
}
int day = list.size();
// 遍历7个和尚
for (int i = 0; i < 7; i++) {
if (visited[i]) {
// 这个和尚已经被安排
continue;
}
if (input[i][day] == 1) {
// 这个和尚第t天可以挑水
list.add(i);
visited[i] = true;
backTrack();
list.remove(list.size() - 1);
visited[i] = false;
}
}
}
}
package huawei;
import java.util.Arrays;
import java.util.Scanner;
/**
* 笨笨熊打包
*/
public class Benbenxiong {
public static final String INPUT = "10\n3\n9 2 3";
public static void main(String[] args) {
Scanner scanner = new Scanner(INPUT);
int capacity = scanner.nextInt();
int length = scanner.nextInt();
int[] arr = new int[length];
for (int i = 0; i < length; i++) {
arr[i] = scanner.nextInt();
}
System.out.println(makePackage(arr, capacity));
}
private static int makePackage(int[] arr, int v) {
Arrays.sort(arr);
int length = arr.length;
for (int i = 0, j = length - 1; i < j; i++, j--) {
while (i < j && arr[i] + arr[j] > v) {
j--;
}
if (i < j) {
length--;
}
}
return length;
}
}