小明参加公司团建,100个同事围坐一圈,裁判开始顺时针从头发牌,每发三张白牌就会发出一张黑牌,抽到黑牌的人出局,每局第N个抽到黑牌的将获得奖励。问小明想获得奖品,需要坐在最开始100人里第几个位置。
import java.util.Scanner;
public class MiYuesefu {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
//1.构建循环列表
Node<Integer> first = null;
//记录前一个结点
Node<Integer> pre = null;
for (int i = 1; i <= 100; i++) {
//第一个元素
if (i == 1) {
first = new Node<Integer>(i, null);
pre = first;
continue;
}
Node<Integer> node = new Node<Integer>(i, null);
pre.next = node;
pre = node;
if (i == 100) {
pre.next = first;
}
}
//2.使用count,记录当前的报数值
int count = 0;
int countN = 0;
//3.遍历链表,每循环一次,count++
Node<Integer> n = first;
Node<Integer> before = null;
while (n != n.next) {
//4.判断count的值,如果是3,则从链表中删除这个结点并打印结点的值,把count重置为0;
count++;
if (count == 4){
countN++;
if (countN == N){
System.out.println(n.item);
}
//删除当前结点
before.next = n.next;
count=0;
n = n.next;
}else {
before = n;
n = n.next;
}
}
/*
* 打印剩余的最后那个人
* */
//System.out.println(n.item);
}
private static class Node<T> {
//存储数据
T item;
//下一个结点
Node next;
public Node(T item, Node next) {
this.item = item;
this.next = next;
}
}
}