背景
上午我看了一个介绍分形和杨辉三角的视频,其中提到了杨辉三角中数字的奇偶性,我联想到奇偶性和布尔运算有密切的联系,于是写了点代码来展示杨辉三角中数字的奇偶性。
正文
杨辉三角中的每个数字 <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
*
**
* *
****
* *
** **
* * * *
********
* *
** **
* * * *
**** ****
* * * *
** ** ** **
* * * * * * * *
****************
* *
** **
* * * *
**** ****
* * * *
** ** ** **
* * * * * * * *
******** ********
* * * *
** ** ** **
* * * * * * * *
**** **** **** ****
* * * * * * * *
** ** ** ** ** ** ** **
* * * * * * * * * * * * * * * *
********************************
* *
** **
* * * *
**** ****
* * * *
** ** ** **
* * * * * * * *
******** ********
* * * *
** ** ** **
* * * * * * * *
**** **** **** ****
* * * * * * * *
** ** ** ** ** ** ** **
* * * * * * * * * * * * * * * *
**************** ****************
* * * *
** ** ** **
* * * * * * * *
**** **** **** ****
* * * * * * * *
** ** ** ** ** ** ** **
* * * * * * * * * * * * * * * *
******** ******** ******** ********
* * * * * * * *
** ** ** ** ** ** ** **
* * * * * * * * * * * * * * * *
**** **** **** **** **** **** **** ****
* * * * * * * * * * * * * * * *
** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
****************************************************************
* *
** **
* * * *
**** ****
* * * *
** ** ** **
* * * * * * * *
******** ********
* * * *
** ** ** **
* * * * * * * *
**** **** **** ****
* * * * * * * *
** ** ** ** ** ** ** **
* * * * * * * * * * * * * * * *