[C/C++ 演算法]-巴斯卡三角形

[C/C++ 演算法]-巴斯卡三角形

[C/C++ 演算法]-巴斯卡三角形

剛才找資料時發現一個C/C++的教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。
拷貝來源:

http://openhome.cc/Gossip/AlgorithmGossip/

http://openhome.cc/Gossip/AlgorithmGossip/PascalTriangle.htm

#include <stdio.h>
#define HEIGHT 12

int combi(int r, int n){
int p = 1;
int i;
for(i = 1; i <= n; i++) {
p = p * (r - i + 1) / i;
}
return p;
}
int main() {
int r;
for(r = 0; r < HEIGHT; r++) {
char format[5];
sprintf(format, "%%%ds", (HEIGHT - r) * 3);
printf(format, "");
int n;
for(n = 0; n <= r; n++) {
printf("%6d", combi(r, n));
}
printf("\n");
}
return 0;
} 

 

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *