haihongyuan.com
海量文库 文档专家
全站搜索:
您现在的位置:首页 > 小学教育 > 学科竞赛学科竞赛

数3退2

发布时间:2013-12-20 10:42:26  

Java 面向对象设计数3退一

public class Count3Quit1 {

public static void main(String args[]){ KidCircle kc = new KidCircle(10); Kid k = kc.first;

int countnum=0;

while(kc.count > 1){ countnum++; if(countnum == 3 ){ countnum = 0; kc.delete(k); }

k = k.right;

}

System.out.println(kc.first.id); }

}

class Kid {

int id;

Kid left;

Kid right;

}

class KidCircle {

int count=0;

Kid first;

Kid last;

KidCircle(int n){

for(int i=0;i<n;i++){ add();

}

}

void add(){

Kid k = new Kid(); k.id = count;

if(count <= 0){

first = k;

last = k;

k.left = k;

k.right = k;

}else {

last.right = k;

k.left = last;

k.right = first;

first.left = k;

last = k;

}

count++;

}

void delete(Kid k){

if(count <= 0)

System.out.println("没有小孩"); else if(count == 1){

first = last = null; }else {

k.left.right = k.right; k.right.left = k.left; if(k == first){

first = k.right; } else if(k==last){ last = k.left; }

}

count--;

}

}

public class Count3Quit {

public static void main(String[] args) { boolean arr[] = new boolean[500]; for(int i=0; i<arr.length; 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<arr.length; i++) { if(arr[i] == true) {

System.out.println(i); }

}

}

}

网站首页网站地图 站长统计
All rights reserved Powered by 海文库
copyright ©right 2010-2011。
文档资料库内容来自网络,如有侵犯请联系客服。zhit326@126.com