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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

新人求罩啊那个有没有简单的方法

7 个回复

倒序浏览
杨辉三角是什么,楼主可以说的明白点嘛
回复 使用道具 举报
  1. public class Test4 {
  2.         public static void main(String[] args) {
  3.         printPascalTriangle(5);
  4.         
  5.         }
  6.     public static void printPascalTriangle(int level) {
  7.         int[][] triangle = new int[level][level];
  8.         for (int i = 0; i < level; i++) {
  9.             for (int j = 0; j <= i; j++) {
  10.                 if (j == 0 || j == i)
  11.                     triangle[i][j] = 1;
  12.                 else
  13.                     triangle[i][j] = triangle[i - 1][j] + triangle[i - 1][j - 1];
  14.                 System.out.print(triangle[i][j] + " ");
  15.                
  16.             }
  17.             System.out.println();
  18.         }
  19.    
  20.     }
复制代码
回复 使用道具 举报 1 0
  1. /* 杨辉三角时间和空间最优算法 */
  2. import java.util.Scanner;
  3. class YangHuiSanJiao
  4. {
  5.         public static void main(String[] args)
  6.         {
  7.                 Scanner sc = new Scanner(System.in);
  8.                 System.out.print("请输入需要打印的杨辉三角的高度:");
  9.                 int s = 1, h = sc.nextInt();                    // 数值和高度, 输入层数
  10.                 for(int i = 0; i < h; i++)
  11.                 {
  12.                         System.out.print("   ");
  13.                 }
  14.                 System.out.println(" 1");                  // 输出第一个 1
  15.                 for (int i = 2; i <= h; s = 1, i++)         // 行数 i 从 2 到层高
  16.                 {
  17.                         for(int j = i; j <= h; j++)
  18.                         {
  19.                                 System.out.print("   ");
  20.                         }
  21.                         System.out.print(" "+1);
  22.                         if(i == 2)
  23.                         {
  24.                                 System.out.print("     ");
  25.                         }
  26.                         for (int j = 1; j <= i - 2; j++) // 列位置 j 绕过第一个直接开始循环
  27.                         {
  28.                                 s = (i - j) * s / j;
  29.                                 if(j == 1)
  30.                                 {
  31.                                         for(int k = 0; k < 6 - (s+"").length(); k++){
  32.                                                 System.out.print(" ");
  33.                                         }
  34.                                 }
  35.                                 System.out.print(s + " ");
  36.                                 for(int k = 0; k < 5 - (s+"").length(); k++){
  37.                                         System.out.print(" ");
  38.                                 }
  39.                         }
  40.                         System.out.println(1+"   ");
  41.                 }
  42.         }

  43. }
复制代码


等腰显示自己调整吧,太蛋疼了……
回复 使用道具 举报
谢谢大神好赞
回复 使用道具 举报
import java.util.Scanner;
class Test{
        public static void main(String[] args) {               
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入二维数组长度");
                int a = sc.nextInt();
                System.out.println("开始打印了:哈哈");
                chushihua(a);               
        }
                /*  要求打印:杨辉三角形
                1
                1 1
                1 2 1
                1 3 3 1
                1 4 6 4 1               
                */
                //打印方法
        public static void print(int[][] arr){                               
                for(int i = 0; i < arr.length; i++){  //2
                        for(int j = 0; j < arr[i].length; j++){//1
                                arr[i][0] = 1;
                                arr[i][arr[i].length - 1] = 1;
                                if(arr[i][j]==0){
                                        arr[i][j] = arr[i-1][j-1] + arr[i-1][j];1
                                }
                                System.out.print(arr[i][j] + "\t");
                        }                       
                        System.out.println();               
                }               
        }

        //初始化数组
        public static void chushihua(int a){
                int[][] aaa = new int[a][];
                for(int i = 0; i < aaa.length; i++){
                        aaa[i] = new int[i+1];                       
                }               
                print(aaa);       
        }

}
回复 使用道具 举报
张乐 中级黑马 2015-4-24 23:02:41
7#
刘镓旗 发表于 2015-4-24 22:53
import java.util.Scanner;
class Test{
        public static void main(String[] args) {               

这个同学的代码很简洁  看起来简单明了.帮顶
回复 使用道具 举报
都是大神啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马