ให้ n C r โดยที่ C แทนการรวมกัน n แทนจำนวนทั้งหมด และ r แทนการเลือกจากชุด ภารกิจคือการคำนวณมูลค่าของ nCr
ชุดค่าผสมคือการเลือกข้อมูลจากที่ให้ไว้ใน a โดยไม่คำนึงถึงการจัดเรียง การเรียงสับเปลี่ยนและการรวมกันต่างกันในแง่ที่ว่าการเรียงสับเปลี่ยนเป็นกระบวนการของการจัดเรียง ในขณะที่การรวมกันเป็นกระบวนการของการเลือกองค์ประกอบจากชุดที่กำหนด
สูตรการเรียงสับเปลี่ยนคือ -:
nPr = (n!)/(r!*(n-r)!)
ตัวอย่าง
Input-: n=12 r=4 Output-: value of 12c4 is :495
อัลกอริทึม
Start Step 1 -> Declare function for calculating factorial int cal_n(int n) int temp = 1 Loop for int i = 2 and i <= n and i++ Set temp = temp * i End return temp step 2 -> declare function to calculate ncr int nCr(int n, int r) return cal_n(n) / (cal_n(r) * cal_n(n - r)) step 3 -> In main() declare variable as int n = 12, r = 4 print nCr(n, r) Stop
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
//it will calculate factorial for n
int cal_n(int n){
int temp = 1;
for (int i = 2; i <= n; i++)
temp = temp * i;
return temp;
}
//function to calculate ncr
int nCr(int n, int r){
return cal_n(n) / (cal_n(r) * cal_n(n - r));
}
int main(){
int n = 12, r = 4;
cout <<"value of "<<n<<"c"<<r<<" is :"<<nCr(n, r);
return 0;
} ผลลัพธ์
value of 12c4 is :495