现在的位置: 首页 > 综合 > 正文

数3退1

2013年06月19日 ⁄ 综合 ⁄ 共 1096字 ⁄ 字号 评论关闭
/* 数3退1 */
/**
*    n个人围成一圈,循环数数(1,2,3),当数到3时,此人退出,剩下的人继续
* 当只剩下一个人时,请问该人在当初是第几个位置。
*/

public class Count3Quit {
    
public static void main(String[] args) {
        
/**
        * 首先声明布尔类型的数组,设为true,表示在圈里
        * 当为false时,表示此人不在圈里了
        
*/

        
boolean[] arr = new boolean[2];
        
for (int i = 0; i < arr.length; i++{
            arr[i] 
= true;
        }

        
        
//圈里还剩下的人数
        int leftCount = arr.length;
        
//数数1,2,3
        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 < arr.length; i++{
            
if (arr[i] == true{
                System.out.println(i);
            }

        }

    }

}

 

抱歉!评论已关闭.