java杨辉三角

杨辉三角规律:

  1. 第一行有1个元素,第n行有n个元素;//列数不确定的二维数组
  2. 每一行的每一个元素和最后一个元素都是1;
  3. 从第三行开始,对于非第一个元素和最后一个元素的元素的值,
    arr[i][j] = arr[i-1][j] + arr[i-1][j-1]
java 复制代码
//2024.07.03

public class YangHui {

	public static void main(String[] args) {

		int[][] yangHui = new int[10][];

		for (int i = 0; i < yangHui.length; i++) {
			yangHui[i] = new int[i + 1];

			for (int j = 0; j < yangHui[i].length; j++) {
				if (j == 0 || j == yangHui[i].length - 1) {
					yangHui[i][j] = 1;
				}else{
					yangHui[i][j] = yangHui[i-1][j] + yangHui[i-1][j-1]; 
				}
				
			}
		}

		for (int i = 0; i < yangHui.length; i++) {
			for (int j = 0; j < yangHui[i].length; j++) {
				System.out.print(yangHui[i][j] + "\t");//遍历输出
				
			}
			System.out.println();
		}
	}
}
相关推荐
neo_Ggx23几秒前
Maven 版本管理详解:SNAPSHOT、Release 与 Nexus 仓库的区别和影响
java·maven
matlabgoodboy4 分钟前
软件开发定制小程序APP帮代做java代码代编写C语言设计python编程
java·c语言·小程序
IronMurphy9 分钟前
【算法四十五】139. 单词拆分
算法
江离w15 分钟前
新版vibecoding项目初始化指令
java
yuan1999717 分钟前
基于 C# 实现的 Omron HostLink (FINS) 协议 PLC 通讯
开发语言·c#
tongluowan00742 分钟前
Spring MVC 底层工作流程+源码分析
java·spring·mvc
qq_422828621 小时前
android图形学之SurfaceControl和Surface的关系 五
android·开发语言·python
洛水水1 小时前
【力扣100题】32.将有序数组转换为二叉搜索树
数据结构·算法·leetcode
java1234_小锋1 小时前
SpringBoot为什么要禁止循环依赖?
java·数据库·spring boot
如竟没有火炬2 小时前
用队列实现栈
开发语言·数据结构·python·算法·leetcode·深度优先