Java数三退一问题代码_用Java实现数三退一的算法中一些问题

这是一个算法,请各位看看错的为什么错了,小弟感激不尽!正确的:publicclassCount3Quit{publicstaticvoidmain(String[]args){boolean[]arr=newboolean[500];for(inti=0;i

这是一个算法,请各位看看错的为什么错了,小弟感激不尽!

正确的:

public class Count3Quit {

public static void main(String[] args) {

boolean[] arr = new boolean[500];

for(int i=0; i

arr[i] = true;

}

int leftCount = arr.length;

int countNum = 0;

int index = 0;

while(leftCount > 1) {

if(arr[index] == true) {

countNum ++;

if(countNum == 3) {

countNum = 0;

arr[index] = false;

leftCount --;

}

}

index ++;

if(index == arr.length) {

index = 0;

}

}

for(int i=0; i

if(arr[i] == true) {

System.out.println(i);

}

}

}

}

错误的:

public class MyCount3Quit {

public static void main(String[] args) {

boolean[] array = new boolean[3]; //创建Boolean类型的数组array,长度为500;

for(int i = 0; i

array[i] = true;

//System.out.println(array[i]);

}

int length = array.length; //定义length的长度等于array的长度;

int index =0;

int count =0; //定义下标值index,和用于数数的Count;

while(length>1) { //当length的长度大于1时执行while语句

if(array[index] == true) {

count++; //每次循环过来Count的值加一;

if(count==3) { //如果下标在逻辑上等于3了,

array[index] = false; //array[index]的值改为FALSE;

count = 0; //count归零;

length--; //数到3一次,数组长度就减一;

}

}

if(index==array.length) { //当下标值等于和数组的长度减一相等了,下标值从零开始

index = 0;

}

index++; //每循环一次,代表数组下标的index的值加一;

}

for(int i = 0; i < array.length; i++) { //用for语句打印出array[i]为TRUE的数字;

if(array[i] == true) {

System.out.println(i);

}

}

}

}

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值