A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 廉伟 中级黑马   /  2012-8-29 08:26  /  1835 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /*
  2. 提示:杨辉三角是由一个由数字排列的三角形数字表,特征两侧数字为1,其余每个数值为其正上方元素

  3. 值与左上方元素值之和。
  4. 可用数组array[i][j]=array[i-1][j-1]+array[i-1][j]来表示。
  5. */
  6. import java.util.Scanner;

  7. public class YangHuiTriangle {

  8.        
  9.         public static void main(String[] args) {
  10.                
  11.                 //输入行数
  12.                 System.out.println("请输入杨辉三角的行数:");
  13.                 Scanner ScRows=new Scanner(System.in);
  14.                 final int Rows=ScRows.nextInt();
  15.                
  16.                 //声明二维数组,设置一维行数为Rows+1
  17.                 int array[][] =new int[Rows+1][];
  18.                
  19.                 //循环初始化数组
  20.                 for(int i=0;i<=Rows;i++){
  21.                         //设置数组的二位行数
  22.                         array[i]=new int[i+1];
  23.                        
  24.                 }
  25.                
  26.                 System.out.println("杨辉三角为:");
  27.                 YhTriangle(array,Rows);
  28.         }

  29.         //输出杨辉三角
  30.         public static void YhTriangle(int array[], int rows) {

  31.                 //行控制
  32.                 for(int i=0;i<=rows;i++){
  33.                         //列控制
  34.                         for(int j=0;j<array[i].length;j++){
  35.                                
  36.                                 //赋值给二位数组,将两边的元素赋值为1
  37.                                 if(i==0||j==0||j==array[i].length-1)
  38.                                        
  39.                                         array[i][j]=1;
  40.                                 //将其正上方元素与左上角元素之和赋值给此元素中。       
  41.                                 else
  42.                                        
  43.                                         array[i][j]=array[i-1][j-1]+array[i-1][j];
  44.                                
  45.                         }
  46.                 }
  47.                
  48.                 //打印输出杨辉三角
  49.                 for(int i=0;i<=rows;i++){
  50.                        
  51.                         for(int j=0;j<array[i].length-1;j++){
  52.                                
  53.                                 System.out.print(array[i][j]" ");       
  54.                                
  55.                         }
  56.                        
  57.                         System.out.println();
  58.                 }
  59.         }

  60. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

2 个回复

倒序浏览
  1. /*

  2. 提示:杨辉三角是由一个由数字排列的三角形数字表,特征两侧数字为1,其余每个数值为其正上方元素


  3. 值与左上方元素值之和。

  4. 可用数组array[i][j]=array[i-1][j-1]+array[i-1][j]来表示。

  5. */

  6. import java.util.Scanner;


  7. public class YangHuiTriangle {


  8.         
  9.         public static void main(String[] args) {

  10.                
  11.                 //输入行数

  12.                 System.out.println("请输入杨辉三角的行数:");

  13.                 Scanner ScRows=new Scanner(System.in);

  14.                 final int Rows=ScRows.nextInt();

  15.                
  16.                 //声明二维数组,设置一维行数为Rows+1

  17.                 //int array[][] =new int[Rows+1][];
  18.                                 int[][] array = new int[Rows][Rows];
  19.                 /*
  20.                 //循环初始化数组

  21.                 for(int i=0;i<=Rows;i++){

  22.                         //设置数组的二位行数

  23.                         array[i]=new int[i+1];

  24.                         
  25.                 }*/

  26.                
  27.                 System.out.println("杨辉三角为:");

  28.                 YhTriangle(array,Rows);

  29.         }


  30.         //输出杨辉三角

  31.         public static void YhTriangle(int array[][], int rows) {                           //这里传入的第一个参数是二维数组


  32.                 array[0][0]=1;
  33.                 //行控制
  34.                                
  35.                 for(int i=1;i<rows;i++){                                       //这里i的初值改了

  36.                         //列控制

  37.                         for(int j=0;j<=i;j++){

  38.                                 
  39.                                 //赋值给二位数组,将两边的元素赋值为1

  40.                                 /*if(i==0||j==0||j==array[i].length-1)

  41.                                        
  42.                                         array[i][j]=1;*/
  43.                                 if(j==0 || i==j)
  44.                                                 array[i][j]=1;

  45.                                 //将其正上方元素与左上角元素之和赋值给此元素中。        
  46.                                 else

  47.                                        
  48.                                         array[i][j]=array[i-1][j-1]+array[i-1][j];

  49.                                 
  50.                         }

  51.                 }

  52.                
  53.                 //打印输出杨辉三角

  54.                 for(int i=0;i<rows;i++){

  55.                         
  56.                         for(int j=0;j<=i;j++){                                         //这里循环的遍历条件改了

  57.                                 
  58.                                 System.out.print(array[i][j]+"        ");        //这里有“+”号
  59.                                 
  60.                         }

  61.                         
  62.                         System.out.println();

  63.                 }

  64.         }


  65. }
复制代码
改的地方有点多,不过总算是能打印出杨辉三角了,希望你能读懂,要是有些不足还望指出

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报
谢谢你了啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马