春晚刘谦撕纸牌魔术模拟程序

开发
春晚上刘谦的两个魔术表演都非常精彩,尤其是第二个魔术,他演绎了经典的约瑟夫环问题。作为一名程序员我们尝试从编程的角度来揭秘刘谦的魔术。

春晚上刘谦的两个魔术表演都非常精彩,尤其是第二个魔术,他演绎了经典的约瑟夫环问题。作为一名程序员我们尝试从编程的角度来揭秘刘谦的魔术。

约瑟夫环

约瑟夫环(Josephus problem)是一个著名的理论问题,它描述的是这样一个场景:n个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围,从编号为k的人开始报数,数到m的那个人出圈,他的下一个人又从1开始报数,数到m的那个人又出圈,依此规律重复下去,直到剩余最后一个胜利者。这个问题在计算机科学和数学中都有广泛的应用,其在计算机编程的算法中又称为约瑟夫环或“丢手绢问题”。

魔术流程

  1. 4张牌对折后撕开,就是8张,叠放在一起就是ABCDABCD。注意,ABCD四个数字是完全等价的。
  2. 根据名字字数,把顶上的牌放到下面,但怎么放都不会改变循环序列的相对位置。譬如2次,最后变成CDABCDAB;譬如3次,最后换成DABCDABC。但无论怎么操作,第4张和第8张牌都是一样的。
  3. 把顶上3张插到中间任意位置。这一步非常重要!因为操作完之后必然出现第1张和第8张牌是一样的!以名字两个字为例,可以写成BxxxxxxB(这里的x是其他和B不同的牌)。
  4. 拿掉顶上的牌放到一边,记为B。剩下的序列是xxxxxxB,一共7张牌。
  5. 南方人/北方人/不确定,分别拿顶上的1/2/3张牌插到中间,但是不会改变剩下7张牌是xxxxxxB的结果。
  6. 男生拿掉1张,女生拿掉2张。也就是男生剩下6张,女生剩下5张。分别是xxxxxB和xxxxB。
  7. 循环7次,把最顶上的放到最底下,男生和女生分别会是xxxxBx和xxBxx。
  8. 最后执行约瑟夫环过程,操作到最后只剩下1张。当牌数为6时(男生),剩下的就是第5张牌;当牌数为5时(女生),剩下的就是第3张牌。就是第4步拿掉的那张牌!

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<String> listPoker = new ArrayList<>(54);


    public static List<String> choose = new ArrayList<>(4);

    public static HashMap<Integer, String> map = new HashMap<>();

    static {
        map.put(1, "南方");
        map.put(2, "北方");
        map.put(3, "不确定");
    }


    public static List<String> 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 < 4; i++) {
            System.out.println("\n请选择花色");
            Scanner scanner = new Scanner(System.in);
            styleIn = scanner.nextLine();
            System.out.println("请选择数字");
            numIn = scanner.nextLine();
            System.out.println("是否确认选择 ok or any keys");
            String sc = scanner.nextLine();
            if (!Objects.equals(sc, "ok")) {
                return;
            }
            while (Objects.isNull(styleIn) || Objects.isNull(numIn)) {
                System.out.println("输入错误,请重新输入");
            }
            String chPoker = styleIn + numIn;
            while (!listPoker.contains(chPoker)) {
                System.out.println("花色非法或不存在的数字");
            }
            System.out.println("\n 本次选择" + chPoker);
            choose.add(chPoker);
        }
        System.out.println("你选择的扑克分别为:");
        choose.forEach(System.out::println);
    }

    public static void suffer() {
        System.out.println("\n 请打乱刚刚选择的牌,按ok键进行 !");
        Scanner scanner = new Scanner(System.in);
        if ("ok".equalsIgnoreCase(scanner.nextLine())) {
            System.out.println("\n洗牌前顺序");
            choose.forEach(s -> System.out.println(s + " "));
            Collections.shuffle(choose);
            System.out.println("\n洗牌后顺序");
            choose.forEach(s -> System.out.println(s + " "));
        }
    }

    public static List<String> push() {
        System.out.println("请从中间撕碎扑克,按 ok 撕碎 !");
        Scanner scanner = new Scanner(System.in);
        if (!"ok".equalsIgnoreCase(scanner.nextLine())) {
            System.out.println("不撕就别玩,结束了");
            return choose;
        }
        ArrayList<String> 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<String> 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<String> 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<String> push, boolean region, int regionChoose) {
        Random random = new Random();

        if (region) {
            regionChoose = (regionChoose < 1 || regionChoose > 3) ? 3 : regionChoose;
        }

        // 生成3到5之间的随机数
        int randomNum = region ? random.nextInt(4 + (3 - regionChoose)) : random.nextInt(4);
        System.out.println(randomNum);
        ArrayList<String> 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<String> 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<String> pushed, int i) {
        for (int i1 = 0; i1 < i; i1++) {
            pushed.remove(0);
        }
    }

    public static int chooseSex() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择 性别 男:1 女:2");
        int i = scanner.nextInt();
        System.out.println(i == 1 ? "男" : "女");
        return i;
    }

    /**
     * 好运留下来
     *
     * @param args
     */
    public static void luck(List<String> push) {
        String first = push.get(0);
        push.remove(0);
        System.out.println("好运留下来");
        push.add(first);
        System.out.println("烦恼丢出去");
        push.remove(0);
    }

    private static void anyWay(List<String> pushed) {
        while (pushed.size() > 1) {
            luck(pushed);
        }
    }


    public static void main(String[] args) {
        // 获取完整扑克
        getAllPoker().forEach(t -> System.out.print("   " + t));
        // 从中选择四张
        choosePoker();
        // 打乱
        suffer();
        // 撕碎
        List<String> 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));
    }
}
责任编辑:赵宁宁 来源: 沐雨花飞蝶
相关推荐

2024-03-11 15:54:29

C++代码

2024-02-04 07:00:00

机器人魔术

2022-12-13 09:00:16

编程技术Nygaard模拟程序

2012-01-05 10:25:04

2021-02-08 23:10:08

春晚红包互联网

2009-04-13 10:21:45

网管春晚摩卡软件

2021-02-19 18:04:17

Python春晚数据

2016-06-23 15:44:44

大数据

2021-07-15 14:29:06

LRU算法

2014-02-18 14:29:41

Windows Azu微软CNTV

2023-12-27 09:00:00

Python魔术方法开发

2021-09-06 08:13:35

APM系统监控

2017-02-14 14:23:52

大数据春晚

2019-02-01 08:28:42

春晚红包百度红包流量

2022-02-03 14:59:13

互联网春晚流量

2011-09-15 09:50:33

2010-11-03 10:31:51

PHP魔术方法

2021-02-02 10:47:55

云计算8K牛年春晚

2021-09-19 11:10:40

万兆宽带千兆宽带毛谦

2022-07-26 13:00:01

安全符号源代码
点赞
收藏

51CTO技术栈公众号