62,634
社区成员




public class Cycle {
static class Entry{
int num;
Entry next;
}
private static Entry createCycle(int size){
Entry header = new Entry();
header.num=1;
Entry last = header;
for(int i=2;i<=size;i++){
Entry e = new Entry();
e.num=i;last.next=e;
last = e;
}
last.next=header;
return last;
}
public static void main(String[] args) {
final int SIZE = 100;
final int LEN = 7 ;
Entry e = createCycle(SIZE);
int lineNumbers = 0;
while(e.next!=e){
for(int i=LEN;i>1;i--){
e=e.next;
}
Entry ne = e.next;
System.out.printf("%4d",new Object[]{Integer.valueOf(ne.num)});
if(lineNumbers++%10==9)System.out.println();
e.next=ne.next;
}
System.out.printf("%4d",new Object[]{Integer.valueOf(e.num)});
}
}
package cn.fee.arithmetic;
public class Quit7Test {
public static void main(String[] args) {
boolean[] kid = new boolean[100];
for(int i = 0; i < kid.length; i++) {
kid[i] = true;
}
int countNum = 100;
int number = 0;
int index = 0;
while(countNum > 1) {
if(kid[number] == true ) {
index++;
if(index == 7) {
kid[number] = false;
countNum--;
index = 0;
}
}
number++;
if(number == 100) {
number = 0;
}
}
for(int i = 0; i < kid.length; i++) {
if(kid[i] == true) {
System.out.println(i);
}
}
}
}
import java.util.*;
public class Test1{
public static void main(String[] args){
//josephus(8,3);
josephus(100,7);
}
public static void josephus(int num,int k){
LinkedList<Integer> linkedList=new LinkedList<Integer>();
for(int i=0;i<num;i++){
linkedList.add(i+1);
}
int index=0;
int count=0; //return new line for each 10 result.
while(linkedList.size()>0){
if(count%10==0) System.out.println();
index=(index+k-1)%linkedList.size();
System.out.printf("%4d",linkedList.get(index));
linkedList.remove(index);
count++;
}
System.out.println();
}
}
package selfimpr.test;
/**
*
* @author selfimpr
* @mail lgg860911@yahoo.com.cn
* @blog http://blog.csdn.net/lgg201
*/
public class Algorithm {
/**
* @param args
* 有100个数字组成的序列,
* 从序列第1个数字开始数,数到7的时候
* 把对应的数字从序列中删除,
* 当数到最后1个数字的时候,
* 跳到第1个数字,如此一直循环,直到把所有数字删除。
*/
public static void main(String[] args) {
Sequence seq = init();
int i = 0;
while(seq.first != null) {
seq.getNext();
i++;
System.out.print(seq.current.data + "-");
if(i%7==0) {
seq.remove();
}
}
}
private static Sequence init() {
Sequence seq = new Sequence(1);
for(int i = 2; i<=100; i++) {
seq.add(i);
}
return seq;
}
}
/**
* 模拟题目要求的数据结构序列
* @author selfimpr
* @mail lgg860911@yahoo.com.cn
* @blog http://blog.csdn.net/lgg201
*/
class Sequence {
Node first;
Node previou;
Node current;
int i = 0;
Sequence(long data) {
first = new Node(data);
current = first;
}
/*
* 添加一个数据为data的节点在Sequence末尾
*/
void add(long data) {
current.next = new Node(data);
current = current.next;
previou = current;
}
/*
* 移除当前节点current
*/
void remove() {
System.out.println(current.data + " is removed, all removed count is: " + ++i);
if(previou == null && current == first) {
first = current = current.next;
return ;
}
previou.next = current.next;
current = previou;
}
/*
* 获取序列的下一个节点, 并移动序列的current和previou指针
*/
Node getNext() {
if(current.next == null) {
previou = null;
current = first;
} else {
previou = current;
current = current.next;
}
return current;
}
}
/**
* 序列的节点
* @author selfimpr
* @mail lgg860911@yahoo.com.cn
* @blog http://blog.csdn.net/lgg201
*/
class Node {
Node next;
long data;
Node(long data) {
this.data = data;
}
}