一道无数答案的题,帮忙解一下
提示: 作者被禁止或删除 内容自动屏蔽
public class Test { private void test(int num){ for(int i=0;i<5;i++){ System.out.println("第"+i+1+"次"); System.out.println("num="+num); num--; System.out.println("扔掉一个="+num); System.out.println("平分5份,取走一份,为:"+num/5); num=num*4/5; } } public static void main(String[] args) { int num = 5; int i; int remain; for (int j = 1;; j++) { remain = 4 * j; for (i = 0; i < num; i++) { if (remain % 4 == 0) { remain = remain * 5 / 4 + 1; } else break; } if (i == num) { System.out.println(remain); break; } } Test t = new Test(); t.test(remain); } }
public class Test { private void test(int num){ for(int i=0;i<5;i++){ System.out.println("第"+i+1+"次"); System.out.println("num="+num); num--; System.out.println("扔掉一个="+num); System.out.println("平分5份,取走一份,为:"+num/5); num=num*4/5; } } public static void main(String[] args) { int num = 5; int i; int remain; boolean b = false; for (int j = 1;; j++) { remain = 4 * j; for (i = 0; i < num; i++) { if (remain % 4 == 0) { remain = remain * 5 / 4 + 1; b=true; } else{ b=false; break; } } if (i == num&&!b) { System.out.println(remain); break; } } Test t = new Test(); t.test(remain); } }
public class Test { private void test(int num){ for(int i=0;i<5;i++){ System.out.println("第"+i+1+"次"); System.out.println("num="+num); num--; System.out.println("扔掉一个="+num); System.out.println("平分5份,取走一份,为:"+num/5); num=num*4/5; } } public static void main(String[] args) { int num = 5; int i; int remain; boolean b = false; for (int j = 1;; j++) { remain = 4 * j; for (i = 0; i < num; i++) { if (remain % 4 == 0) { remain = remain * 5 / 4 + 1; b=true; } else{ b=false; break; } } if (i == num&&b) { System.out.println(remain); break; } } Test t = new Test(); t.test(remain); } }
public class Test { private boolean isRight(int num){ if((num-1)%5==0&&num>1) return true; else return false; } private void test(int num){ StringBuffer str = new StringBuffer(); for(int i=0;i<5;i++){ if(isRight(num)){ str.append("第"+(i+1)+"次\n"); str.append("num="+num+"\n"); num--; str.append("扔掉一个="+num+"\n"); str.append("平分5份,取走一份,为:"+num/5+"\n"); num=num*4/5; }else{ str.delete(0, str.length()); break; } } System.out.print(str); } public static void main(String[] args) { int num = 5; int i; int remain; boolean b = false; for (int j = 1;; j++) { remain = 4 * j; for (i = 0; i < num; i++) { if (remain % 4 == 0) { remain = remain * 5 / 4 + 1; b=true; } else{ b=false; break; } } if (i == num&&b) { System.out.println(remain); break; } } Test t = new Test(); for(int j=1;j<3123; j++){ t.test(j); } } }