源程序揭秘
楊輝三角形性質(zhì):
每行數(shù)字左右對(duì)稱,由 1 開始逐漸變大,然后變小,回到 1。
第 n 行的數(shù)字個(gè)數(shù)為 n 個(gè)。
第 n 行數(shù)字和為 2^(n-1) 。
每個(gè)數(shù)字等于上一行的左右兩個(gè)數(shù)字之和。可用此性質(zhì)寫出整個(gè)楊輝三角形。
第 n 行的第 1 個(gè)數(shù)為 1,第二個(gè)數(shù)為 1× (n-1) ,第三個(gè)數(shù)為 1× (n-1) × ( n-2) /2,第四個(gè)數(shù)為
1× (n-1) × (n-2) /2× (n-3) /3…依此類推。
算法原理1:
使用一個(gè)二維數(shù)組 yh[][] 存儲(chǔ)楊輝三角形的數(shù)據(jù),行和列的大小為所需要輸出的行數(shù) Row(本程
序中 Row 為 10)。
使用 for 循環(huán)使楊輝三角中除了最外層(不包括楊輝三角底邊)的數(shù)為 1 ;
使用語(yǔ)句 yh[i][j] = yh[i - 1][j - 1] + yh[i - 1][j] 使第 i 行第 j 列的數(shù)據(jù)等于第(i-1) 行
第(j-1)列的數(shù)據(jù)與第(i-1)行第(j)列的數(shù)據(jù)之和,即每個(gè)數(shù)字等于上一行的左右兩個(gè)數(shù)字之和。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
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(); } } } |
方式二
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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 要理解下面的實(shí)現(xiàn),首先要明白int數(shù)組中元素默認(rèn)值為 0 * 2 然后每一次迭代打印新的一行的元素的時(shí)候: * 新的特定位置的元素 = 該位置原來(lái)的元素 + 該位置的前一個(gè)位置的值 */ </span></code></pre> public static void printYFTriangle(){ System.out.println( "楊輝三角,您準(zhǔn)備輸出的行數(shù):" ); Scanner input = new Scanner(System.in); int lines = input.nextInt(); //獲得循環(huán)的行數(shù); int[] a = new int[lines + 1];//臨時(shí)存儲(chǔ)數(shù)據(jù)用; int previous = 1; //默認(rèn)第一個(gè)數(shù); for (int i = 1; i <= lines; i ++){//i 用來(lái)控制行數(shù); for(int j=1;j<=lines-i;j++){//輸出空格,很easy; System.out.print(" "); } for (int j = 1; j <= i; j++){ int current = a[j];//先獲得后一個(gè)數(shù), a[j] = previous + current; previous = current; System.out.print(a[j] + " "); } System.out.println(); }}} |
方法三:遞歸實(shí)現(xiàn)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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( "請(qǐng)輸入行數(shù):" ); 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(); } } } |
請(qǐng)輸入行數(shù):
6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
再分享一個(gè)實(shí)例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
/** * 打印楊輝三角形(帕斯卡三角形),打印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(); } } } |
結(jié)果:
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
總結(jié)
以上就是本文關(guān)于Java編程實(shí)現(xiàn)帕斯卡三角形代碼示例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
原文鏈接:http://blog.csdn.net/zhangchen124/article/details/74840688