展示杨辉三角中数字的奇偶性

背景

上午我看了一个介绍分形和杨辉三角的视频,其中提到了杨辉三角中数字的奇偶性,我联想到奇偶性和布尔运算有密切的联系,于是写了点代码来展示杨辉三角中数字的奇偶性。

正文

杨辉三角中的每个数字 <math xmlns="http://www.w3.org/1998/Math/MathML"> n u m num </math>num 都等于它左上角的数字 <math xmlns="http://www.w3.org/1998/Math/MathML"> L L </math>L 和右上角数字 <math xmlns="http://www.w3.org/1998/Math/MathML"> R R </math>R 的和(如果一个数字左上角没有数字或者右上角没有数字,那么这个数字的值为 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 \text{1} </math>1)。如果我们只关注数字的奇偶性,那么可以这样分析 ⬇️ (下表的分析仅针对 <math xmlns="http://www.w3.org/1998/Math/MathML"> L L </math>L 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> R R </math>R 都存在的情况)

<math xmlns="http://www.w3.org/1998/Math/MathML"> L L </math>L 是否为奇数 <math xmlns="http://www.w3.org/1998/Math/MathML"> R R </math>R 是否为奇数 <math xmlns="http://www.w3.org/1998/Math/MathML"> n u m = L + R num=L+R </math>num=L+R 是否为奇数

于是可以写出这样的代码 ⬇️

java 复制代码
public class Analyzer {

    public void displayRow(boolean[] currValues, int n) {
        for (int i = 1; i <= n; i++) {
            System.out.print(currValues[i] ? '*' : ' ');
        }
        System.out.println();
    }

    public void analyze(int bound) {
        int n = 1;
        // currValues[0] and currValues[len - 1] are always false
        boolean[] currValues = new boolean[]{false, true, false};
        while (true) {
            displayRow(currValues, n);
            if (++n == bound) {
                break;
            }
            currValues = calcNextValues(currValues, n);
        }
    }

    private boolean[] calcNextValues(boolean[] currValues, int n) {
        boolean[] nextValues = new boolean[n + 2];
        for (int i = 1; i <= n; i++) {
            nextValues[i] = currValues[i - 1] != currValues[i];
        }
        return nextValues;
    }

    public static void main(String[] args) {
        new Analyzer().analyze(80);
    }
}

请将以上代码保存为 Analyzer.java。运行如下命令就可以编译 Analyzer.java

bash 复制代码
javac Analyzer.java

运行如下命令可以运行 Analyzer 中的 main 方法。

bash 复制代码
java Analyzer

在我电脑上的运行结果如下 ⬇️

text 复制代码
*
**
* *
****
*   *
**  **
* * * *
********
*       *
**      **
* *     * *
****    ****
*   *   *   *
**  **  **  **
* * * * * * * *
****************
*               *
**              **
* *             * *
****            ****
*   *           *   *
**  **          **  **
* * * *         * * * *
********        ********
*       *       *       *
**      **      **      **
* *     * *     * *     * *
****    ****    ****    ****
*   *   *   *   *   *   *   *
**  **  **  **  **  **  **  **
* * * * * * * * * * * * * * * *
********************************
*                               *
**                              **
* *                             * *
****                            ****
*   *                           *   *
**  **                          **  **
* * * *                         * * * *
********                        ********
*       *                       *       *
**      **                      **      **
* *     * *                     * *     * *
****    ****                    ****    ****
*   *   *   *                   *   *   *   *
**  **  **  **                  **  **  **  **
* * * * * * * *                 * * * * * * * *
****************                ****************
*               *               *               *
**              **              **              **
* *             * *             * *             * *
****            ****            ****            ****
*   *           *   *           *   *           *   *
**  **          **  **          **  **          **  **
* * * *         * * * *         * * * *         * * * *
********        ********        ********        ********
*       *       *       *       *       *       *       *
**      **      **      **      **      **      **      **
* *     * *     * *     * *     * *     * *     * *     * *
****    ****    ****    ****    ****    ****    ****    ****
*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *
**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
****************************************************************
*                                                               *
**                                                              **
* *                                                             * *
****                                                            ****
*   *                                                           *   *
**  **                                                          **  **
* * * *                                                         * * * *
********                                                        ********
*       *                                                       *       *
**      **                                                      **      **
* *     * *                                                     * *     * *
****    ****                                                    ****    ****
*   *   *   *                                                   *   *   *   *
**  **  **  **                                                  **  **  **  **
* * * * * * * *                                                 * * * * * * * *
相关推荐
databook1 天前
告别手动计算,SymPy 初识与 Manim 联动
python·数学·动效
Luhui Dev2 天前
手机上怎么画几何图?适合学生的在线几何作图工具(2026)
数学·大角几何·luhuidev
Tisfy5 天前
LeetCode 2553.分割数组中数字的数位:模拟(maybe+翻转)——java也O(1)
java·数学·算法·leetcode·题解·模拟·取模
大卫小东(Sheldon)6 天前
霍尔定理和最大流算法 入门
数学
闻缺陷则喜何志丹9 天前
【高等数学 第十二章】无穷级数
数学·高等数学·幂级数·傅里叶级数·无穷级数
Luhui Dev10 天前
几何题目自动配图技术详解:从文本到图形的智能化方案
人工智能·数学·luhuidev
闻缺陷则喜何志丹12 天前
【高等数学 十一章】曲线积分与曲面积分
数学·高等数学·曲面积分·曲线积分
闻缺陷则喜何志丹12 天前
【高等数学 第十章】重积分
数学·高等数学·重积分
AKDreamer_HeXY14 天前
QOJ 12255 - 36 Puzzle 题解
数据结构·c++·数学·算法·icpc·qoj