揭秘 2024 春晚刘谦魔术——代码还原

其他系列文章导航

Java基础合集
数据结构与算法合集

设计模式合集

多线程合集

分布式合集

ES合集


文章目录

其他系列文章导航

文章目录

前言

一、魔术大概流程

二、代码实现各个步骤

2.1 partition(对半撕牌)

2.2 bottom(将 n 张牌置底)

2.3 insert(三张牌随机插入到五张牌中间)

2.4 pop(丢第一张)

2.5 throwOneOrTwo(男生丢一张牌,女生丢两张)

2.6 mysticalWords(七字真言)

2.7 LeaveOneGoOne (置底一张丢一张)

三、整体代码及结果

四、总结


前言

随着 2024 年春晚的落幕,刘谦的魔术表演再次成为了人们热议的焦点。

从 2009 年到 2019 年,刘谦 10 年间 5 次亮相央视春晚舞台,一句“见证奇迹的时刻”成为刘谦的招牌台词。但从 2019 年在春晚表演《魔壶》之后,刘谦好像销声匿迹了,连续 5 年都与春晚无缘。2024 年 2 月 9 日晚,刘谦终于带着最新魔术节目《守岁共此时》再次亮相春晚,神乎其技的表现让观众直呼不可思议。

今天,我将尝试从编程的角度来揭秘刘谦的魔术,通过代码实现来解析其背后的原理。


一、魔术大概流程

观众们按照一定的顺序撕开扑克牌,通过名字字数、男女性别、南方北方等关键词进行排序筛选,最终丢弃了大部分的碎牌。而剩下的两张碎牌,竟然神奇地凑成了一张完整的牌。

大致流程:

  1. 随机选取四张牌。
  2. 对半撕开并叠在一起。
  3. 按照名字的长度把最上面的牌放到底部。
  4. 最上面三张牌随机插入到五张牌中间。
  5. 把最上面一张牌藏屁股下。
  6. 男生扔掉最上面一张牌,女生扔两张。
  7. 喊出七字真言,每喊一个字将一张牌置底。
  8. 然后置底一张丢一张,直到还剩一张。


二、代码实现各个步骤

2.1 partition(对半撕牌)

    static int[] partition(int[] arr) {
        int[] newArr = new int[arr.length * 2];
        System.arraycopy(arr, 0, newArr, 0, arr.length);
        System.arraycopy(arr, 0, newArr, 4, arr.length);
        return newArr;
    }
  • 目的:将输入的数组arr复制到一个新的数组中,并将arr的元素再复制一次到新的数组中的特定位置。
  • 实现:创建一个新数组newArr,其长度是arr的两倍。然后,使用System.arraycopy方法将arr的元素复制到newArr的前半部分和从索引4开始的位置。

 2.2 bottom(将 n 张牌置底)

    static int[] bottom(int[] arr, int n) {
        int[] newArr = new int[arr.length];
        System.arraycopy(arr, n, newArr, 0, arr.length - n);
        System.arraycopy(arr, 0, newArr, arr.length - n, n);
        return newArr;
    }
  • 目的:将arr数组中的前n个元素移动到数组的末尾,并将其余的元素移到数组的开头。
  • 实现:使用System.arraycopy方法实现元素的移动。

2.3 insert(三张牌随机插入到五张牌中间)

    static int[] insert(int[] arr) {
        int[] newArr = new int[arr.length];
        int n = new Random().nextInt(4) + 1;
        System.arraycopy(arr, 3, newArr, 0, n);
        System.arraycopy(arr, 0, newArr, n, 3);
        System.arraycopy(arr, 3 + n, newArr, 3 + n, arr.length - n - 3);
        return newArr;
    }
  • 目的:将arr数组中的前三个元素随机插入到数组的中间位置。
  • 实现:首先,生成一个1到4之间的随机数n。然后,使用System.arraycopy方法将前三个元素插入到新数组的随机位置,并将其余的元素放在正确的位置。

 2.4 pop(丢第一张)

    static int[] pop(int[] arr) {
        int[] newArr = new int[arr.length - 1];
        System.arraycopy(arr, 1, newArr, 0, arr.length - 1);
        return newArr;
    }
  • 目的:移除arr数组的第一个元素,并返回剩余的元素。
  • 实现:创建一个新的数组newArr,其长度比arr少1。然后,使用System.arraycopy方法将arr从索引1开始的所有元素复制到newArr

2.5 throwOneOrTwo(男生丢一张牌,女生丢两张)

    static int[] throwOneOrTwo(int[] arr, String gender) {
        if (gender.equals("女")) {
            int[] newArr = new int[arr.length - 2];
            System.arraycopy(arr, 2, newArr, 0, arr.length - 2);
            return newArr;
        } else {
            return pop(arr);
        }
    }
  • 目的:根据提供的性别(男或女)移除数组中的元素。如果是女性,则移除前两个元素;如果是男性,则移除第一个元素。
  • 实现:根据性别,调用pop方法或创建一个新的数组并复制从索引2开始的所有元素。

2.6 mysticalWords(七字真言)

     static int[] mysticalWords(int[] arr) {
        if (arr.length == 5) {
            return bottom(arr, 2);
        } else {
            return bottom(arr, 1);
        }
    }
  • 目的:根据数组的长度,将数组中的元素移动到特定的位置。如果数组长度为5,则移动最后两个元素到数组的底部;否则,移动最后一个元素到数组的底部。
  • 实现:根据数组的长度,调用bottom方法并传递相应的参数。

2.7 LeaveOneGoOne (置底一张丢一张)

    static int[] LeaveOneGoOne(int[] arr) {
        while (arr.length > 1) {
            int[] bottom = bottom(arr, 1);
            arr = pop(bottom);
        }
        return arr;
    }
  • 目的:不断从数组中移除元素,直到数组中只剩下一个元素。
  • 实现:使用while循环和前面定义的bottompop方法,每次循环都将数组的最后一个元素移动到数组的底部,并从数组中移除它。

三、整体代码及结果

代码:

public class Magic {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        System.out.println("选取四张牌:"+ Arrays.toString(arr));
        int[] partition = partition(arr);
        System.out.println("对半撕开并叠在一起:"+ Arrays.toString(partition));
        int[] bottom = bottom(partition, 3);
        System.out.println("按照名字的长度把最上面的牌放到底部:"+ Arrays.toString(bottom));
        int[] insert = insert(bottom);
        System.out.println("最上面三张牌随机插入到五张牌中间:"+ Arrays.toString(insert));
        System.out.println("现在最上面的一张牌是:"+ insert[0]);
        int[] pop = pop(insert);
        System.out.println("把最上面一张牌藏屁股下,还剩:"+ Arrays.toString(pop));
        int[] throwOneOrTwo = throwOneOrTwo(pop, "女");
        System.out.println("男生扔掉最上面一张牌,女生扔两张:"+ Arrays.toString(throwOneOrTwo));
        int[] mysticalWords = mysticalWords(throwOneOrTwo);
        System.out.println("喊出七字真言,每喊一个字将一张牌置底"+ Arrays.toString(mysticalWords));
        int[] leaveOneGoOne = LeaveOneGoOne(mysticalWords);
        System.out.println("然后开始留下一张置底丢一张,直到还剩一张:"+ leaveOneGoOne[0]);
    }

    static int[] partition(int[] arr) {
        int[] newArr = new int[arr.length * 2];
        System.arraycopy(arr, 0, newArr, 0, arr.length);
        System.arraycopy(arr, 0, newArr, 4, arr.length);
        return newArr;
    }

    //下放 n 个
    static int[] bottom(int[] arr, int n) {
        int[] newArr = new int[arr.length];
        System.arraycopy(arr, n, newArr, 0, arr.length - n);
        System.arraycopy(arr, 0, newArr, arr.length - n, n);
        return newArr;
    }

    //最上面三个随机插入到五个中间
    static int[] insert(int[] arr) {
        int[] newArr = new int[arr.length];
        int n = new Random().nextInt(4) + 1;
        System.arraycopy(arr, 3, newArr, 0, n);
        System.arraycopy(arr, 0, newArr, n, 3);
        System.arraycopy(arr, 3 + n, newArr, 3 + n, arr.length - n - 3);
        return newArr;
    }

    //第一个去掉,藏屁股底下
    static int[] pop(int[] arr) {
        int[] newArr = new int[arr.length - 1];
        System.arraycopy(arr, 1, newArr, 0, arr.length - 1);
        return newArr;
    }

    //男生扔1,女生扔2
    static int[] throwOneOrTwo(int[] arr, String gender) {
        if (gender.equals("女")) {
            int[] newArr = new int[arr.length - 2];
            System.arraycopy(arr, 2, newArr, 0, arr.length - 2);
            return newArr;
        } else {
            return pop(arr);
        }
    }

    //七字真言
    static int[] mysticalWords(int[] arr) {
        if (arr.length == 5) {
            return bottom(arr, 2);
        } else {
            return bottom(arr, 1);
        }
    }

    //留下一个走一个
    static int[] LeaveOneGoOne(int[] arr) {
        while (arr.length > 1) {
            int[] bottom = bottom(arr, 1);
            arr = pop(bottom);
        }
        return arr;
    }
}

结果: 


四、总结

刘谦老师的魔术本质其实就是约瑟夫环的问题。

我认为这是语言类节目第一名。

充满了真情实感的欢声笑语。大家的参与性很高,过大年,追求的不过是热热闹闹,开开心心,这个节目做到了。


相关推荐

  1. 2024魔术C++实现

    2024-02-15 20:08:02       59 阅读
  2. Python实现2024魔术

    2024-02-15 20:08:02       47 阅读
  3. 龙年魔术模拟

    2024-02-15 20:08:02       49 阅读
  4. 魔术解析Python

    2024-02-15 20:08:02       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-15 20:08:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-15 20:08:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-15 20:08:02       87 阅读
  4. Python语言-面向对象

    2024-02-15 20:08:02       96 阅读

热门阅读

  1. git错误整理

    2024-02-15 20:08:02       44 阅读
  2. ES实战-高级聚合

    2024-02-15 20:08:02       46 阅读
  3. linux系统zabbix监控自定义监控

    2024-02-15 20:08:02       52 阅读
  4. 力扣-344. 反转字符串

    2024-02-15 20:08:02       44 阅读
  5. TDC - Crisis/Phobia - 4 types of Crisis

    2024-02-15 20:08:02       45 阅读
  6. Spring Cloud Ribbon:负载均衡

    2024-02-15 20:08:02       36 阅读
  7. centos7.9 搭建k8s

    2024-02-15 20:08:02       52 阅读
  8. 盘点五种常用的数据加密技术

    2024-02-15 20:08:02       46 阅读
  9. Win 10 如何升级 Win 11

    2024-02-15 20:08:02       57 阅读
  10. 数据结构基础

    2024-02-15 20:08:02       45 阅读
  11. 跟着pink老师前端入门教程-day25

    2024-02-15 20:08:02       59 阅读