ให้ด้วยค่าจำนวนเต็มบวก สมมุติว่า 'val' และงานคือการพิมพ์ค่าของสัมประสิทธิ์ทวินาม B(n, k) โดยที่ n และ k เป็นค่าใดๆ ระหว่าง 0 ถึง val และด้วยเหตุนี้จึงแสดงผลลัพธ์
สัมประสิทธิ์ทวินามคืออะไร
สัมประสิทธิ์ทวินาม (n, k) คือลำดับของการเลือกผลลัพธ์ 'k' จากความเป็นไปได้ของ 'n' ที่ให้มา ค่าสัมประสิทธิ์ทวินามของค่าบวก n และ k ถูกกำหนดโดย
$$C_k^n=\frac{n!}{(n-k)!k!}$$
โดยที่ n>=k
ตัวอย่าง
Input-: B(9,2) Output-:
$$B_2^9=\frac{9!}{(9-2)!2!}$$
$$\frac{9\times 8\times 7\times 6\times 5\times 4\times 3\times 2\times 1}{6\times 5\times 4\times 3\times 2\times 1)\ คูณ 2\times 1}=\frac{362,880}{1440}=252$$
ตารางค่าสัมประสิทธิ์ทวินามคืออะไร
ตารางค่าสัมประสิทธิ์ทวินามถูกสร้างขึ้นสำหรับการคำนวณค่าหลายค่าที่สามารถสร้างได้ระหว่าง n และ k
ตัวอย่าง
Input-: value = 5 Output-:

แนวทางที่ใช้ในโปรแกรมด้านล่างมีดังนี้ −
- ป้อนตัวแปร 'val' จากผู้ใช้เพื่อสร้างตาราง
- เริ่มการวนซ้ำจาก 0 ถึง 'val' เพราะค่าสัมประสิทธิ์ทวินามจะอยู่ระหว่าง 0 ถึง 'val'
-
ใช้สูตรที่กำหนด ถ้า n และ k ไม่ใช่ 0
B(ม., x) =B(ม., x - 1) * (ม. - x + 1) / x
- พิมพ์ผลลัพธ์
อัลกอริทึม
START Step 1-> declare function for binomial coefficient table int bin_table(int val) Loop For int i = 0 and i <= val and i++ print i Declare int num = 1 Loop For int j = 0 and j <= i and j++ If (i != 0 && j != 0) set num = num * (i - j + 1) / j End print num End print \n Step 2-> In main() Declare int value = 5 call bin_table(value) STOP
ตัวอย่าง
#include <stdio.h>
// Function for binomial coefficient table
int bin_table(int val) {
for (int i = 0; i <= val; i++) {
printf("%2d", i);
int num = 1;
for (int j = 0; j <= i; j++) {
if (i != 0 && j != 0)
num = num * (i - j + 1) / j;
printf("%4d", num);
}
printf("\n");
}
}
int main() {
int value = 5;
bin_table(value);
return 0;
} ผลลัพธ์
