程序代码:
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);
}
}
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);
}
}
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);
}
}
----------------解决方案--------------------------------------------------------
回复 2楼 linjx0123
提示: 作者被禁止或删除 内容自动屏蔽