春晚上刘谦的两个魔术表演都非常精彩,尤其是第二个魔术,他演绎了经典的约瑟夫环问题。作为一名程序员我们尝试从编程的角度来揭秘刘谦的魔术。
约瑟夫环
约瑟夫环(Josephus problem)是一个著名的理论问题,它描述的是这样一个场景:n个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围,从编号为k的人开始报数,数到m的那个人出圈,他的下一个人又从1开始报数,数到m的那个人又出圈,依此规律重复下去,直到剩余最后一个胜利者。这个问题在计算机科学和数学中都有广泛的应用,其在计算机编程的算法中又称为约瑟夫环或“丢手绢问题”。
魔术流程
Java代码
import java.util.*;
public class CWMS {
static String[] num = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
static String[] style = {"♠️", "♥️", "♣️", "♦️"};
static String[] wang = {"大王", "小王"};
public static List listPoker = new ArrayList(54);
public static List choose = new ArrayList(4);
public static HashMap map = new HashMap();
static {
map.put(1, "南方");
map.put(2, "北方");
map.put(3, "不确定");
}
public static List getAllPoker() {
for (String string : style) {
for (String s : num) {
listPoker.add(string + s);
}
}
listPoker.add(Arrays.toString(wang));
return listPoker;
}
public static void choosePoker() {
String styleIn, numIn;
for (int i = 0; i System.out.println(s + " "));
Collections.shuffle(choose);
System.out.println("n洗牌后顺序");
choose.forEach(s -> System.out.println(s + " "));
}
}
public static List push() {
System.out.println("请从中间撕碎扑克,按 ok 撕碎 !");
Scanner scanner = new Scanner(System.in);
if (!"ok".equalsIgnoreCase(scanner.nextLine())) {
System.out.println("不撕就别玩,结束了");
return choose;
}
ArrayList result = new ArrayList(choose);
for (String s : choose) {
String str = s + "副本";
result.add(str);
}
result.forEach(System.out::println);
return result;
}
public static void nameSuffer(List push) {
System.out.println("请输入你的姓名,名字有几个字,就将最上面的牌放到最下边几次");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
if (Objects.isNull(name)) {
System.out.println("姓名为空");
return;
}
for (int i = 0; i < name.length(); i++) {
String first = push.get(0);
push.remove(0);
push.add(first);
}
}
public static void magicTime(List push) {
String magic = "见证奇迹的时刻";
for (int i = 0; i < magic.length(); i++) {
String first = push.get(0);
push.remove(0);
push.add(first);
}
}
public static void suffer_3(List push, boolean region, int regionChoose) {
Random random = new Random();
if (region) {
regionChoose = (regionChoose 3) ? 3 : regionChoose;
}
// 生成3到5之间的随机数
int randomNum = region ? random.nextInt(4 + (3 - regionChoose)) : random.nextInt(4);
System.out.println(randomNum);
ArrayList third = new ArrayList();
for (int i = 0; i < 3; i++) {
third.add(push.get(i));
}
if (!region) {
push.remove(0);
push.remove(0);
push.remove(0);
} else {
for (int i = 0; i < regionChoose; i++) {
push.remove(0);
}
}
push.addAll(randomNum + 1, third);
}
public static String getFirst(List suffered) {
return suffered.get(0);
}
public static int region() {
System.out.println("n 请选择南北方人 ,如果你是南方人请输入 1;如果你是北方人请输入 2; 如果不能确认你是南北方人请输入 3");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
while (Objects.isNull(map.get(i))) {
System.out.println("输入错误,请重新输入 !");
i = scanner.nextInt();
}
System.out.println("你已选择" + map.get(i));
return i;
}
public static void drop(List pushed, int i) {
for (int i1 = 0; i1 1) {
luck(pushed);
}
}
public static void main(String[] args) {
// 获取完整扑克
getAllPoker().forEach(t -> System.out.print(" " + t));
// 从中选择四张
choosePoker();
// 打乱
suffer();
// 撕碎
List pushed = push();
// name
nameSuffer(pushed);
// suffer_3
suffer_3(pushed, false, 0);
// 记住取出的第一张牌
String first = getFirst(pushed);
// 南北方人选择
// 南方北方切牌
suffer_3(pushed, true, region());
// 男女选择
int sex = chooseSex();
drop(pushed, sex);
// 见证奇迹的时刻
magicTime(pushed);
// 多来几次
anyWay(pushed);
// 对比
System.out.println("第一张牌" + first);
System.out.println("丢完剩下的" + pushed.get(0));
}
}