270 likes | 705 Views
巴斯卡三角形 ( Pascal Triangle ) 蔡文能 tsaiwn@cs.nctu.edu.tw. 課程資訊請進入學校 bb 系統 http://bb.nctu.edu.tw/. m 取 n 有幾種情行 ?. 寫一個程式 , 讀入整數 m 與 n, 然後印出 m 取 n 有幾種情行的答案 已知答案可用 long 表示 已知 m 與 n 最大為 100 提示 : 用 Pascal Triangle ( 不必用乘法 ) 不要企圖去算 n 階乘 , 因為用 long 算到 13 階乘就爆掉了 !.
E N D
巴斯卡三角形 (Pascal Triangle) 蔡文能 tsaiwn@cs.nctu.edu.tw 課程資訊請進入學校 bb 系統http://bb.nctu.edu.tw/
m 取 n有幾種情行? • 寫一個程式, 讀入整數 m 與 n, 然後印出m 取 n 有幾種情行的答案 • 已知答案可用 long 表示 • 已知 m 與 n 最大為 100 • 提示: 用 Pascal Triangle (不必用乘法) • 不要企圖去算 n 階乘, 因為用 long 算到 13階乘就爆掉了!
巴斯卡三角形 http://en.wikipedia.org/wiki/Pascal_triangle
(a+b)2 = a2+2a.b+b2 (a+b)3 = a3+3a2b+3ab2+b3 p7.c 巴斯卡三角形練習 Array (陣列) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 By Blaise Pascal, 1653 C(5,0) = 1 C(5,1) = 5 C(5,2) = 10 = C(5,3) http://en.wikipedia.org/wiki/Pascal_triangle
#define MAX 19 int x[MAX][MAX]; int x[19][19]; 巴斯卡三角形, 方便寫程式 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 除了最左最右的 1, 其他都是等於正上方加左上角
中間每格等於正上方加左上角 • 每列最左與最右都是 1 • x[r][0]=1; x[r][r]=1; // C/C++ 從 0 開始 • 第 r 列第 c 格 = 正上方+左上角 • x[r][c]= x[r-1][c] + x[r-1][c-1];
int x[19][19]; 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 21 }; /* for i */ 1 3 31 1 4 6 41 第 j 格 第 i 列
問題與思考 (Pascal 三角形 ) 其實只用一維陣列(one dimentional array) 就可以做出上述的巴斯卡三角形 Why? 因為是橫著填 .. 填完一列就可印出 • 程式要如何修改? • 提示: 中間的要由右而左填 ! • ? 巴斯卡三角形在數學上有哪些意義? • 二項式, 組合數學: m 取 n 有幾種組合
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.
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
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.
First element elements Last element Name of array y … y[0] y[1] y[2] y[10] index int y[11]; 陣列 Array (1/2) • Index (Subscript) • Element—every element has same type 38 49 250 99
陣列 Array (2/2) • 將元素 ai 置於陣列的第 i 個位置上 ( i 即為index; 又稱 subscript) • 若要讀取 ai時,可利用 ai 的相對位址等於陣列的起始位址加 i*s 來求得 • 其中s是每一元素所佔空間的大小,用byte為單位 • C/C++/Java 陣列的index從0開始
11 22 33 88 -1 a a[0] a[1] a[2] a[3] a[4] 宣告/定義 陣列並初始化 • int a [5] = {11,22,33,88,-1}; • Declare/define and initialize • int b[5] = {38, 49}; // 不夠的自動補 0 • int b[5] = {38, 49, 0, 0, 0};
二維陣列 (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
Row major 以列為主 float y[u1][ u2]; u2個元素 u2個元素 … … 第0列 第1列 第 i 列 第u1-1列 共有( i * u2)個元素 float y[u1][ u2]; 所以當一個二維陣列 y [0 : u1-1, 0 : u2-1] 由圖知 y[i, j]位址在α+ i*u2*s + j*s,其中α為此陣列最開始元素的位址; s 為每個元素佔用的 byte 數。(例如 float佔 4 bytes)
Column major 記憶體安排法(FORTRAN 語言用) REAL y(u1, u2) Column(行) FORTRAN 的 array index 原則上從1 用起 Row(列)
for Loop vs. while Loop 這三個寫法意義完全一樣 for(i=1 ; i<= 9 ; i++) { /* Loop body */ } i=1; for( ; i<= 9 ; ) { /* Loop body */ i++; } i=1; while( i<= 9 ) { /* Loop body */ i++; }
for Loop vs. while Loop for( ; ; ) ; ; while( ) { ; ; } == 0 != 0
while Loop (1/3) 拿盤子, 拿飲料, 找好位子; while(肚子還餓) { 吃一盤; 喝一杯; 喘一口氣; } 結帳; 回家; All you can eat !
while Loop (2/3) 拿盤子, 拿飲料, 找好位子; while(肚子還餓) { 吃一盤; if(有急事 || 很飽了)break; if(不會渴) continue; 喝一杯; 喘一口氣; } 結帳; 回家; All you can eat !
while Loop (3/3) 拿盤子, 拿飲料, 找好位子; while(不夠本) { 吃一盤; if(有急事 || 很飽了)break; if(不會渴) continue; 喝一杯; 喘一口氣; } 結帳; 回家;
Thank You! http://tsaiwn.net/cpp/ 謝謝捧場 tsaiwn@csie.nctu.edu.tw 蔡文能