当前位置:网站首页>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();
}
}
}
边栏推荐
- Jmeter--- set proxy recording request
- c sqlite ... ...
- Imitation Tiktok homepage interface
- SQL中in的用法 DQL 查询
- Redis是什么?简述它的优缺点
- Open source RSS subscriber freshrss
- After 2 years of functional testing, I feel like I can't do anything. Where should I go in 2022?
- JMeter websocket接口测试
- 3. Editors (vim)
- 成为比开发硬气的测试人,我都经历了什么?
猜你喜欢

Animation curves are used every day. Can you make one by yourself? After reading this article, you will!

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

『Skywalking』.NET Core快速接入分布式链路追踪平台

【GO基础02】第一个程序

Application of breakthrough thinking in testing work
![[assembly language 01] basic knowledge](/img/df/d586288b8f41211141bc4e2bca20cf.png)
[assembly language 01] basic knowledge

『SignalR』.NET使用 SignalR 进行实时通信初体验
![[go basics 02] the first procedure](/img/af/f32762a828f384bf6aa063ebf959aa.png)
[go basics 02] the first procedure

Redis foundation 2 (notes)

Jmeter---设置代理录制请求
随机推荐
Detailed summary of C language game dual cache to solve the flash screen problem [easy to understand]
Excuse me, how to deal with repeated consumption of MySQL data
All you want to know about interface testing is here
6-18漏洞利用-后门连接
The file cannot be saved (what if the folder is damaged and cannot be read)
kubernetes之VictoriaMetrics单节点
若依如何解决导出使用下载插件出现异常?
Uninstall NPM and install NPM_ Use 'NPM uninstall' to uninstall the NPM package 'recommended collection'
ZigBee development board (nxpzigbee Development)
Nuclear power plants strive to maintain safety in the heat wave sweeping Europe
[Fantan] how to design a test platform?
sql语句练习题整理
JMeter websocket接口测试
【饭谈】那些看似为公司着想,实际却让人无法理解的事(二:面试时的软素质“眼缘”)
How to implement an app application to limit users' time use?
Basic knowledge in the project
这次龙蜥展区玩的新花样,看看是谁的 DNA 动了?
还不懂mock测试?一篇文章带你熟悉mock
C语言:随机生成数+冒泡排序
虚拟内存与磁盘