Java編程實現帕斯卡三角形程式碼範例,java帕斯卡

來源:互聯網
上載者:User

Java編程實現帕斯卡三角形程式碼範例,java帕斯卡

來源程式揭秘

楊輝三角形性質:

每行數字左右對稱,由 1 開始逐漸層大,然後變小,回到 1。
第 n 行的數字個數為 n 個。
第 n 行數字和為 2^(n-1) 。
每個數字等於上一行的左右兩個數字之和。可用此性質寫出整個楊輝三角形。
第 n 行的第 1 個數為 1,第二個數為 1× (n-1) ,第三個數為 1× (n-1) × ( n-2) /2,第四個數為
1× (n-1) × (n-2) /2× (n-3) /3…依此類推。

演算法原理1:

使用一個二維數組 yh[][] 儲存楊輝三角形的資料,行和列的大小為所需要輸出的行數 Row(本程
序中 Row 為 10)。
使用 for 迴圈使楊輝三角中除了最外層(不包括楊輝三角底邊)的數為 1 ;
使用語句 yh[i][j] = yh[i - 1][j - 1] + yh[i - 1][j] 使第 i 行第 j 列的資料等於第(i-1) 行
第(j-1)列的資料與第(i-1)行第(j)列的資料之和,即每個數字等於上一行的左右兩個數字之和。

package com.work;   public class YangHuiSanJiao  {      public static void main(String[] args) {      int [][]a = new int [10][10];        for(int n = 0; n < 10;n++)        {         a[n][0] = 1;         a[n][n] = 1;        }        for(int n = 2; n <10; n++)        {        for(int j = 1; j < n; j++)        {         a[n][j] = a[n -1][j -1] + a[n - 1][j];        }        }         for(int n = 0; n < 10; n++)        {        for(int k = 0; k < 2 * (10 - n) - 1; k++)        {         System.out.print(" ");        }        for(int j = 0; j <= n; j++)        {         System. out.print(a[n][j] + "  ");        }        System.out.println();        }    }  } 

方式二

package com.face;  import java.util.Scanner;  public class YangHui {   public static void main(String[] args) {     printYFTriangle();   }   <pre code_snippet_id="2474965" snippet_file_name="blog_20170708_2_9005712" class="prettyprint" name="code"><code class="hljs java has-numbering"><span class="hljs-javadoc">/**    * 1 要理解下面的實現,首先要明白int數組中元素預設值為 0    * 2 然後每一次迭代列印新的一行的元素的時候:    * 新的特定位置的元素 = 該位置原來的元素 + 該位置的前一個位置的值    */</span></code></pre>public static void printYFTriangle(){    System.out.println("楊輝三角,您準備輸出的行數:");    Scanner input = new Scanner(System.in);    int lines = input.nextInt();//獲得迴圈的行數;    int[] a = new int[lines + 1];//臨時儲存資料用;    int previous = 1; //預設第一個數;    for (int i = 1; i <= lines; i ++){//i 用來控制行數;      for(int j=1;j<=lines-i;j++){//輸出空格,很easy;        System.out.print(" ");     }      for (int j = 1; j <= i; j++){        int current = a[j];//先獲得後一個數,        a[j] = previous + current;        previous = current;        System.out.print(a[j] + " ");      }      System.out.println();    }}} 

方法三:遞迴實現

package com.face;  import java.util.Scanner;  public class DiGui {   static int fun(int n,int k){     //n,行,k:列     if(k==1||n==k)       return 1;     else       return fun(n-1,k-1)+fun(n-1,k);   }   public static void main(String[] args) {     int lines;     System.out.println("請輸入行數:");     Scanner input=new Scanner(System.in);     lines=input.nextInt();     for(int i=1;i<=lines;i++){       for(int k=1;k<lines-i+1;k++){         System.out.print(" ");       }       for(int j=1;j<=i;j++){         System.out.print(fun(i,j)+" ");       }       System.out.println();     }   } } 

請輸入行數:

6
     1 
    1 1 
   1 2 1 
  1 3 3 1 
 1 4 6 4 1 
1 5 10 10 5 1 

再分享一個執行個體:

/** * 列印楊輝三角形(帕斯卡三角形),列印10行 * */public class Yanghuisanjiao {   public static void main(String[] args) {    int [][] a = new int[11][11];    for (int i = 0 ; i < 10 ; i++) {      a[i][0] = 1;      a[i][i] = 1;    }         for (int i = 1 ; i < 10 ; i ++) {      for (int j = 1; j < i ; j++) {        a[i][j] = a[i-1][j-1] + a[i-1][j];      }    }    for (int i = 0; i < 10 ; i++) {      for (int j = 0; j < 10-i;j++) {        System.out.print(" ");      }      for (int k = 0; k < 10;k++) {        if (a[i][k] != 0) {          System.out.print(a[i][k]+" ");        }               }      System.out.println();    }  } }

結果:

          1
         1 1
        1 2 1
       1 3 3 1
      1 4 6 4 1
     1 5 10 10 5 1
    1 6 15 20 15 6 1
   1 7 21 35 35 21 7 1
  1 8 28 56 70 56 28 8 1
 1 9 36 84 126 126 84 36 9 1

總結

以上就是本文關於Java編程實現帕斯卡三角形程式碼範例的全部內容,希望對大家有所協助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.