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();
		}
	}
}
相关推荐
爱喝水的鱼丶几秒前
SAP-ABAP:ALV通用封装实践——搭建可复用的ALV开发工具类,减少80%重复代码
开发语言·性能优化·sap·abap·erp·alv
钱六两10 分钟前
#3、SpringAI 接入deepSeek大模型(喂饭版)
java·spring boot·ai编程
脱胎换骨-军哥22 分钟前
C++/Rust无缝互操作:混合系统新常态
开发语言·c++·rust
songroom42 分钟前
Kimi K3:Rust封装XTP接口详细教程实践
开发语言·后端·rust
kebeiovo1 小时前
游戏服务端开发:Actor模型详解(Go语言)
开发语言·后端·golang
香辣牛肉饭1 小时前
【算法】动态规划 最长公共子序列(LCS)
经验分享·笔记·算法·动态规划
迷途呀1 小时前
Python:函数中的参数类型
开发语言·笔记·python·langchain·nlp
null_171 小时前
IntelliJ IDEA 极致流畅配置方案:Ultra 9 285K + 64GB 内存实测
java·ide·intellij-idea
Herbert_hwt1 小时前
建立Java程序开发
java·开发语言
好好沉淀2 小时前
@ExcelIgnoreUnannotated 和 @AutoMapper 详解
java