- 打表法
- 整体装袋
- 一般思路
- 打表代码
- 幂次方吃草
- 一般思路
- 打表代码
- 出现和倍数相关的问题时,可以考虑通过一般方法解题后的结果是否存在某种特殊的规律,通过直观的答案直接进行代码的书写,不需要关注本质含义。
public static int num(int apple){
if (apple%8==0)return apple/8;
int n8=apple/8;
int m=apple%8;
while (m<24&&n8>=0){
if (m%6==0)return n8+m/6;
--n8;
m+=8;
}
return -1;
}
打表代码
18前无规律,18后单为-1,双为一个值,每增加8个就增加1
public static int daBiao(int apple){
if (apple<18)return apple == 0 ? 0 : (apple == 6 || apple == 8) ? 1
: (apple == 12 || apple == 14 || apple == 16) ? 2 : -1;
if (apple%2==1)return -1;
return (apple-18)/8+3;
}
幂次方吃草
一般思路
public static String winner(int N) {
if (N <= 4) return N == 0 || N == 2 ? "羊羊" : "牛牛";
int eatTest = 1;
while (eatTest <= N) {
if (winner(N - eatTest).equals("羊羊")) return "牛牛";
eatTest *= 4;
}
return "羊羊";
}
打表代码
public static String win(int N){
return (N)%5==0||(N)%5==2?"羊羊":"牛牛";
}



