巴斯卡三角形 ( pascal triangle ) 蔡文能 [email protected]
DESCRIPTION
巴斯卡三角形 ( Pascal Triangle ) 蔡文能 [email protected]. 課程資訊請進入學校 bb 系統 http://bb.nctu.edu.tw/. m 取 n 有幾種情行 ?. 寫一個程式 , 讀入整數 m 與 n, 然後印出 m 取 n 有幾種情行的答案 已知答案可用 long 表示 已知 m 與 n 最大為 100 提示 : 用 Pascal Triangle ( 不必用乘法 ) 不要企圖去算 n 階乘 , 因為用 long 算到 13 階乘就爆掉了 !. - PowerPoint PPT PresentationTRANSCRIPT
2
m 取 n 有幾種情行 ?
• 寫一個程式 , 讀入整數 m 與 n,
然後印出 m 取 n 有幾種情行的答案• 已知答案可用 long 表示• 已知 m 與 n 最大為 100
• 提示 : 用 Pascal Triangle ( 不必用乘法 )
• 不要企圖去算 n 階乘 , 因為用 long 算到 13 階乘就爆掉了 !
3
巴斯卡三角形
http://en.wikipedia.org/wiki/Pascal_triangle
4
C(m,n) 與 巴斯卡三角形
5
Pascal‘s triangle and Binomial expansions ( 二項式展開示 )
6
p7.c 巴斯卡三角形練習 Array ( 陣列 )
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
C(5,0) = 1 C(5,1) = 5 C(5,2) = 10 = C(5,3)
By Blaise Pascal, 1653
(a+b)2 = a2+2a.b+b2
(a+b)3 = a3+3a2b+3ab2+b3
http://en.wikipedia.org/wiki/Pascal_triangle
7
巴斯卡三角形 , 方便寫程式 0 1 2 3 4 5 6 7 8 . . . 0 1 1 1 1 2 1 2 1 i 3 1 3 3 1 4 1 4 6 4 1 5 1 5 10 10 5 1
int x[19][19];
int x[MAX][MAX];
#define MAX 19
除了最左最右的 1, 其他都是等於正上方加左上角
8
中間每格等於正上方加左上角
• 每列最左與最右都是 1x[r][0]=1; x[r][r]=1; // C/C++ 從 0 開始
• 第 r 列第 c 格 = 正上方 + 左上角x[r][c]= x[r-1][c] + x[r-1][c-1];
9
p7.c 巴斯卡三角形 for(i=0; i<=n; i++) { x[i][0]=1; x[i][i] =1; /* 記得宣告 / 定義 x 陣列
喔 */
for(j=1; j<= i-1; j++) { x[i][j]= x[i-1][j] + x[i-1][j-1]; } for(j =0; j<=i; j++){ 1
printf("%5d", x[i][j]); 1 1 }; printf("\n"); 1 2 1 }; /* for i */ 1 3 3 1 1 4 6 4 1
第
i
列
第 j 格
int x[19][19];
10
問題與思考 (Pascal 三角形 ) 其實只用一維陣列 (one dimentional array)
就可以做出上述的巴斯卡三角形
Why? 因為是橫著填 .. 填完一列就可印出
• 程式要如何修改 ?
• 提示 : 中間的要由右而左填 ! • ? 巴斯卡三角形在數學上有哪些意義 ?
– 二項式 , 組合數學 : m 取 n 有幾種組合
11
Blaise Pascal• Blaise Pascal was born on June 19, 1623 in
Clermont-Ferrand, Puy-de-Dôme, France and died on August 19, 1662 in Paris. Pascal’s mother died when he was three and he was raised and educated by his mathematician father Etienne.
• In honour of Pascal, his name has been given to a unit of pressure, to a computing language and to Pascal’s triangle and Pascal’s wager.
12
Pascal’s Inventions, Theories and Studies
• Pascaline A basic machine for adding and subtracting
• Pascal’s Triangle A mathematical pattern
• Pascal’s Wager A theory on probabilities and an argument for the belief of God
• Hydraulic press Invented the hydraulic press using fluid pressure to multiply force
• Barometer Identified that the level of mercury in a barometer is determined by the
increase or decrease of atmospheric pressure
• Vacuum & Pressure Extensive research into the weight and density of air
13
Pascaline• The first mechanical calculator was invented by Blaise Pascal.• They could only be used for adding and subtracting.
• It was for assisting his father’s workload as a tax commissioner.• He began work on his first calculator in 1642, at the age of 19
and over the next ten years produced fifty prototypes but only sold about a dozen machines.
• The Pascaline (1645) was a decimal machine which was not effective in the French currency system.
• It is the basis for today’s calculators and computer historians recognise his contribution to their field.
14
陣列 Array (1/2)• Index (Subscript)
• Element—every element has same type
38 49 99250
elementsFirst element
Last element
int y[11];
y
y[0] y[2]y[1] y[10]
Name of array
index
…
15
陣列 Array (2/2)
• 將元素 ai 置於陣列的第 i 個位置上 ( i 即為 index; 又稱 subscript)
• 若要讀取 ai 時,可利用 ai 的相對位址等於陣列的起始位址加 i*s 來求得
• 其中 s 是每一元素所佔空間的大小,用 byte 為單位
• C/C++/Java 陣列的 index 從 0 開始
16
宣告 / 定義 陣列並初始化int a [5] = {11,22,33,88,-1};
Declare/define and initialize
int b[5] = {38, 49}; // 不夠的自動補 0int b[5] = {38, 49, 0, 0, 0};
aa[0] a[2]a[1] a[3] a[4]
11 22 33 88 -1
17
二維陣列 (two-dimensional Array)
• type y[u1][u2]; y [0 : u1-1, 0 : u2-1]– 有 u1 row ( 列 ) 與 u2 column ( 欄 )– 每一列是由 u2 個元素組成。
• 二維陣列化成一維陣列時,對映方式有二種:– 以列為主 (row-major) : C/C++/Java– 以行為主 (column-major): FORTRAN
18
Row major 以列為主
u2 個元素 u2 個元素 ……
第 0 列 第 1 列 第 i 列 第 u1-1列
共有 ( i * u2) 個元素
所以當一個二維陣列 所以當一個二維陣列 y [0 : u1-1, 0 : u2-1]y [0 : u1-1, 0 : u2-1]由圖知 由圖知 y[i, j]y[i, j] 位址在位址在 α+ i*uα+ i*u22*s + j*s*s + j*s ,其中,其中 αα為此陣列最開始元素的位址為此陣列最開始元素的位址 ; s ; s 為每個元素佔為每個元素佔用的 用的 byte byte 數數。 ( 例如 float 佔 4 bytes)
float y[u1][ u2];
float y[u1][ u2];
19
Column major 記憶體安排法(FORTRAN 語言用 )
1 2 3 … u2
1 × × × ×
2 × × × ×
3 × × × ×
. . . .
. . . . …
. . . .
u1 × × × ×
Column( 行 )
Row( 列 )
REAL y(u1, u2)FORTRAN 的 array index 原則上從 1 用起
20
for Loop vs. while Loopfor(i=1 ; i<= 9 ; i++){ /* Loop body */}
i=1;for( ; i<= 9 ; ){ /* Loop body */ i++;}
i=1;while( i<= 9 ){ /* Loop body */ i++;}
這三個寫法意義這三個寫法意義完全一樣完全一樣
21
for Loop vs. while Loop
for( ; ; ) ;
;while( ){ ; ;}
== 0
!= 0
22
while Loop (1/3)
拿盤子 , 拿飲料 , 找好位子 ;while( 肚子還餓 ){ 吃一盤 ; 喝一杯 ; 喘一口氣 ;}結帳 ; 回家 ;
All you can eat !
23
while Loop (2/3)
拿盤子 , 拿飲料 , 找好位子 ;while( 肚子還餓 ){ 吃一盤 ; if( 有急事 || 很飽了 )break; if( 不會渴 ) continue; 喝一杯 ; 喘一口氣 ;}結帳 ; 回家 ;
All you can eat !
24
while Loop (3/3)
拿盤子 , 拿飲料 , 找好位子 ;while( 不夠本 ){ 吃一盤 ; if( 有急事 || 很飽了 )break; if( 不會渴 ) continue; 喝一杯 ; 喘一口氣 ;}結帳 ; 回家 ;