java杨辉三角

杨辉三角规律:

  1. 第一行有1个元素,第n行有n个元素;//列数不确定的二维数组
  2. 每一行的每一个元素和最后一个元素都是1;
  3. 从第三行开始,对于非第一个元素和最后一个元素的元素的值,
    arrij = arri-1j + arri-1j-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();
		}
	}
}
相关推荐
平常心的技术小牛9 分钟前
Qt-快速上手-QMenuBar
开发语言·qt
青 春 记 忆34 分钟前
零基础入门Python11|Git实战:为任务管理器建立版本历史
开发语言·git·vscode·python·python3.11
To_OC40 分钟前
LC 239 滑动窗口最大值:从暴力超时到单调队列一遍过
javascript·算法·leetcode
2601_963870201 小时前
【计算机毕业设计】基于Spring Boot的专科医院医疗管理系统
java·spring boot·课程设计
Bingo_BIG1 小时前
Java Spring 批量修改,实体、接口、方法的定义
java·spring
.道阻且长.2 小时前
9.LeetCode算法习题讲解--滑动窗口--无重复字符的最长字串
算法·leetcode·职场和发展·哈希算法
画中有画2 小时前
软件架构中质量属性(性能、安全、可扩展性)的权衡设计
java·运维·安全
Shaoxi Zhang2 小时前
JAVA学习笔记035——对象和JSON格式
java·笔记·学习
赵丙双2 小时前
CountDownLatch 源码分析
java·aqs·countdownlatch
余额瞒着我当琳2 小时前
C++--深拷贝三件套 + swap + 写时拷贝 + vector 扩容 + reserve
java·开发语言·c++