首页 > 其他分享 >杨辉三角

杨辉三角

时间:2022-11-16 22:14:03浏览次数:36  
标签:10 int ++ YangHui length 杨辉三角

import java.util.Scanner;
public class Eext {
public static void main(String[] args) {

//打印一个十行的杨辉三角
/*
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
*/

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 ) { //如果 j 是第一个数或最后一个数 输出 1
YangHui[i][j] = 1;
} else {
YangHui[i][j] = YangHui[i - 1][j] + YangHui[i - 1][j - 1]; //如果 j 是上一行相邻的两个数字之和 输出这行
}
}
}

for (int i = 0; i < YangHui.length; i++) { //输出YangHui三角
for (int j = 0; j < YangHui[i].length; j++) {
System.out.print(YangHui[i][j] + " ");
}
System.out.println();
}
}
}

标签:10,int,++,YangHui,length,杨辉三角
From: https://www.cnblogs.com/shuqiqi/p/16897690.html

相关文章

  • 杨辉三角
    #include<stdio.h>intmain(){ inta[10][10]={}; inti; intj; //给对角线,首列元素赋值为1 for(i=0;i<10;i++){ for(j=0;j<=i;j++){ if(i==j||j=......
  • leetcode java 杨辉三角
    简介杨辉三角是一道简单题,可以通过类似一层推下一层的方式进行计算,但是好像看过一个题解,采用的方式是组合数。本来想采用组合数,尝试了double溢出尝试了long溢出,尝试......
  • 学习笔记:python杨辉三角
    python学习问题输出杨辉三角刚开始着手这题,我先是使用杨辉三角的公式,采用比较简洁的写法进行。defjc(x):r=1forkinrange(1,x+1):r=r*k......
  • LeetCode算法笔记 118. 杨辉三角
    importjunit.framework.TestCase;importjava.util.ArrayList;importjava.util.List;publicclassLeetCode04_2extendsTestCase{/****11......
  • 杨辉三角II
    杨辉三角一、题目描述给定一个非夫的索引rowIndex,返回[杨辉三角]中的,每个数是它左上方和右上方数的和。返回的是给定的索引处的行。实例:输入:rowIndex=3输出:[1,3......
  • 杨辉三角的变形---牛客网
    杨辉三角的变形_牛客题霸_牛客网(nowcoder.com) #include<iostream>usingnamespacestd;intmain(){//这个树的偶数规律为-1-123242324int......
  • C++ 打印杨辉三角/贾宪三角/帕斯卡三角
    #include<iostream>#include<iomanip>#include<windows.h>#include<fstream>#include<string>usingnamespacestd;#defineN10intmain(){inta[N][N......
  • 【Java基础】二维数组实现杨辉三角
    1.什么是杨辉三角每一行头尾都为1,每个数都等于上面两个数之和arr[3][1]=arr[2][0]+arr[2][1];arr[3][2]=arr[2][1]+arr[2][2];2.实现int[][]arr=new......