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();
		}
	}
}
相关推荐
Cyber4K23 分钟前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
梦梦代码精24 分钟前
BuildingAI 上部署自定义工作流智能体:5 个实用技巧
大数据·人工智能·算法·开源软件
Mr_pyx37 分钟前
Spring AI 入门教程:Java开发者的AI应用捷径
java·人工智能·spring
Le_ee1 小时前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php
Zephyr_01 小时前
Leedcode算法题
java·算法
流年如夢2 小时前
栈和列队(LeetCode)
数据结构·算法·leetcode·链表·职场和发展
苍煜2 小时前
Java开发IO零基础吃透:BIO、NIO、同步异步、阻塞非阻塞
java·python·nio
yong99902 小时前
MATLAB读取高光谱图像
开发语言·matlab
2zcode2 小时前
基于MATLAB的肝病风险评估与分期分析系统设计与实现
开发语言·matlab