modify code on class

pull/6/head
左程云 4 years ago
parent 1681840ccc
commit 0ddb8b7bac

@ -6,24 +6,18 @@ public class Code01_AppleMinBags {
if (apple < 0) {
return -1;
}
int bag6 = -1;
int bag8 = apple / 8;
int rest = apple - 8 * bag8;
while (bag8 >= 0 && rest < 24) {
int restUse6 = minBagBase6(rest);
if (restUse6 != -1) {
bag6 = restUse6;
break;
int bag8 = (apple >> 3);
int rest = apple - (bag8 << 3);
while(bag8 >= 0) {
// rest 个
if(rest % 6 ==0) {
return bag8 + (rest / 6);
} else {
bag8--;
rest += 8;
}
rest = apple - 8 * (--bag8);
}
return bag6 == -1 ? -1 : bag6 + bag8;
}
// 如果剩余苹果rest可以被装6个苹果的袋子搞定返回袋子数量
// 不能搞定返回-1
public static int minBagBase6(int rest) {
return rest % 6 == 0 ? (rest / 6) : -1;
return -1;
}
public static int minBagAwesome(int apple) {
@ -38,7 +32,7 @@ public class Code01_AppleMinBags {
}
public static void main(String[] args) {
for(int apple = 1; apple < 100;apple++) {
for(int apple = 1; apple < 200;apple++) {
System.out.println(apple + " : "+ minBags(apple));
}

@ -2,6 +2,27 @@ package class38;
public class Code02_EatGrass {
// 如果n份草最终先手赢返回"先手"
// 如果n份草最终后手赢返回"后手"
public static String whoWin(int n) {
if (n < 5) {
return n == 0 || n == 2 ? "后手" : "先手";
}
// 进到这个过程里来,当前的先手,先选
int want = 1;
while (want <= n) {
if (whoWin(n - want).equals("后手")) {
return "先手";
}
if (want <= (n / 4)) {
want *= 4;
} else {
break;
}
}
return "后手";
}
public static String winner1(int n) {
if (n < 5) {
return (n == 0 || n == 2) ? "后手" : "先手";
@ -29,7 +50,7 @@ public class Code02_EatGrass {
public static void main(String[] args) {
for (int i = 0; i <= 50; i++) {
System.out.println(i + " : " + winner1(i));
System.out.println(i + " : " + whoWin(i));
}
}

@ -3,9 +3,9 @@ package class38;
public class Code03_MSumToN {
public static boolean isMSum1(int num) {
for (int i = 1; i <= num; i++) {
int sum = i;
for (int j = i + 1; j <= num; j++) {
for (int start = 1; start <= num; start++) {
int sum = start;
for (int j = start + 1; j <= num; j++) {
if (sum + j > num) {
break;
}
@ -19,6 +19,12 @@ public class Code03_MSumToN {
}
public static boolean isMSum2(int num) {
//
// return num == (num & (~num + 1));
//
// return num == (num & (-num));
//
//
return (num & (num - 1)) != 0;
}

@ -9,23 +9,60 @@ public class Code04_MoneyProblem {
// 目前你的能力是ability你来到了index号怪兽的面前如果要通过后续所有的怪兽
// 请返回需要花的最少钱数
public static long process(int[] d, int[] p, int ability, int index) {
public static long process1(int[] d, int[] p, int ability, int index) {
if (index == d.length) {
return 0;
}
if (ability < d[index]) {
return p[index] + process(d, p, ability + d[index], index + 1);
} else { // 可以贿赂,也可以不贿赂
return
Math.min(
p[index] + process(d, p, ability + d[index], index + 1),
process(d, p, ability, index + 1)
);
return p[index] + process1(d, p, ability + d[index], index + 1);
} else { // ability >= d[index] 可以贿赂,也可以不贿赂
return Math.min(
p[index] + process1(d, p, ability + d[index], index + 1),
0 + process1(d, p, ability, index + 1));
}
}
public static long func1(int[] d, int[] p) {
return process(d, p, 0, 0);
return process1(d, p, 0, 0);
}
// 从0....index号怪兽花的钱必须严格==money
// 如果通过不了,返回-1
// 如果可以通过,返回能通过情况下的最大能力值
public static long process2(int[] d, int[] p, int index, int money) {
if (index == -1) { // 一个怪兽也没遇到呢
return money == 0 ? 0 : -1;
}
// index >= 0
// 1) 不贿赂当前index号怪兽
long preMaxAbility = process2(d, p, index - 1, money);
long p1 = -1;
if (preMaxAbility != -1 && preMaxAbility >= d[index]) {
p1 = preMaxAbility;
}
// 2) 贿赂当前的怪兽 当前的钱 p[index]
long preMaxAbility2 = process2(d, p, index - 1, money - p[index]);
long p2 = -1;
if (preMaxAbility2 != -1) {
p2 = d[index] + preMaxAbility2;
}
return Math.max(p1, p2);
}
public static int minMoney2(int[] d, int[] p) {
int allMoney = 0;
for (int i = 0; i < p.length; i++) {
allMoney += p[i];
}
int N = d.length;
for (int money = 0; money < allMoney; money++) {
if (process2(d, p, N - 1, money) != -1) {
return money;
}
}
return allMoney;
}
public static long func2(int[] d, int[] p) {
@ -113,7 +150,7 @@ public class Code04_MoneyProblem {
public static void main(String[] args) {
int len = 10;
int value = 20;
int testTimes = 1000000;
int testTimes = 10000;
for (int i = 0; i < testTimes; i++) {
int[][] arrs = generateTwoRandomArray(len, value);
int[] d = arrs[0];
@ -121,7 +158,8 @@ public class Code04_MoneyProblem {
long ans1 = func1(d, p);
long ans2 = func2(d, p);
long ans3 = func3(d, p);
if (ans1 != ans2 || ans2 != ans3) {
long ans4 = minMoney2(d,p);
if (ans1 != ans2 || ans2 != ans3 || ans1 != ans4) {
System.out.println("oops!");
}
}

Loading…
Cancel
Save