java--杨辉三角
生活随笔
收集整理的這篇文章主要介紹了
java--杨辉三角
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package test111111;public class Test11 { public static void main(String[] args){printYanghui();}//先總結一般規律,再討論特殊情況,再做圖形調整public static void printYanghui() {int rowMax = 6; int[][] arr = new int[rowMax][rowMax];for(int row=0;row<rowMax;row++) {for(int k=0;k<rowMax-row;k++) {System.out.print(" ");}for(int col=0;col<=row;col++) {if(col==row||col==0||row==0) { //特殊情況arr[row][col] = 1;}else {arr[row][col] = arr[row-1][col-1]+arr[row-1][col]; //一般規律} System.out.print(arr[row][col]+" ");}System.out.println("");}for(int row=0;row<rowMax;row++) {for(int col=0;col<rowMax;col++) { System.out.print(arr[row][col]+" ");}System.out.println("");} } }
???1
???? 1 1
??? 1 2 1
?? 1 3 3 1
? 1 4 6 4 1
?1 5 10 10 5 1
轉載于:https://www.cnblogs.com/plant/p/7492533.html
總結
以上是生活随笔為你收集整理的java--杨辉三角的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python--12 内嵌函数和闭包
- 下一篇: ACM OJ反馈结果大全