当前位置:网站首页>Output Yang Hui triangle with two-dimensional array
Output Yang Hui triangle with two-dimensional array
2022-07-25 22:08:00 【Bao Zhou Pao】
Punch in the first day of study , Use Java Output Yanghui triangle can customize the number of output lines
Yanghui triangle example :
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
It can be seen that , The rule is that the value of the first element and the last element in each line is 1, Other element values are the sum of the value above the element and the previous value above .
Code implementation :
package array;
import java.util.Scanner;
public class TwoArray02 {
public static void main(String[] args){
Scanner myScanner=new Scanner(System.in);
System.out.println(" Please output the number of lines to be printed :");
int n=myScanner.nextInt();// Enter the number of lines
int[][] arr=new int[n][];// Create a 2D array
for(int i=0;i<arr.length;i++){// Traverse the number of one-dimensional arrays in two-dimensional arrays
arr[i]=new int[i+1];// Open up space for one-dimensional arrays
for(int j=0;j<i+1;j++){// Traverse the element values of each one-dimensional array
if(j==0||j==arr[i].length-1){// The first and last elements of each line are 1
arr[i][j]=1;
}
else{// Other element values are the sum of the value above the element and the previous value above
arr[i][j]=arr[i-1][j]+arr[i-1][j-1];
}
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
边栏推荐
- Uninstall NPM and install NPM_ Use 'NPM uninstall' to uninstall the NPM package 'recommended collection'
- Jenkins+svn configuration
- Redis为何选择单线程?
- El expression improves JSP
- 2022 love analysis ― bank digitalization practice report
- 文件无法保存(文件夹已损坏无法读取怎么办)
- 还不懂mock测试?一篇文章带你熟悉mock
- 数据库进阶·如何针对所有用户数据中没有的数据去加入随机的数据-蜻蜓Q系统用户没有头像如何加入头像数据-优雅草科技kir
- sql语句练习题整理
- Don't know mock test yet? An article to familiarize you with mock
猜你喜欢

Whether the five distribution methods will produce internal fragments and external fragments

Special class design
![[go basics 02] the first procedure](/img/af/f32762a828f384bf6aa063ebf959aa.png)
[go basics 02] the first procedure

JMeter websocket接口测试

Redis usage details

这次龙蜥展区玩的新花样,看看是谁的 DNA 动了?

Victoriametrics single node of kubernetes

少儿编程 电子学会图形化编程等级考试Scratch一级真题解析(判断题)2022年6月

在进行自动化测试,遇到验证码的问题,怎么办?

6-18漏洞利用-后门连接
随机推荐
Three ways to allocate disk space
SQL中in的用法 DQL 查询
C语言:随机生成数+冒泡排序
Acwing 866. determining prime numbers by trial division
『Skywalking』.NET Core快速接入分布式链路追踪平台
Don't know mock test yet? An article to familiarize you with mock
JSP novice
Preliminary study on Tesseract OCR
关于接口测试你想知道的都在这儿了
Solutions to the failure of win key in ikbc keyboard
Having met a tester with three years' experience in Tencent, I saw the real test ceiling
【饭谈】如何设计好一款测试平台?
How to quickly build a picture server [easy to understand]
After 2 years of functional testing, I feel like I can't do anything. Where should I go in 2022?
LeetCode_ 93_ Restore IP address
『SignalR』. Net using signalr for real-time communication
In Oracle 19C version, logminer package continuous_ The outdated function of mine leads to CDC failure
核电站在席卷欧洲的热浪中努力保持安全工作
[Fantan] how to design a test platform?
Detailed summary of C language game dual cache to solve the flash screen problem [easy to understand]