二項式係數在 C++ 中的平方和
二項式係數是二項式定理中找到的一個引號,可以用帕斯卡三角的形式排列,它是數字一種組合,等於 nCr,其中 r 從 n 個專案的集合中選擇,顯示以下公式
nCr=n! / r!(n-r)! or nCr=n(n-1)(n-2).....(n-r+1) / r!
二項式係數的平方和即 (nC0)2 + (nC1)2 + (nC2)2 + (nC3)2 + ……… + (nCn-2)2 + (nCn-1)2 + (nCn)2
Input :n=5 Output:252
說明
在此程式中,我們必須首先找到從 n 個集合中選擇的 r 的二項式係數,然後我們必須平方每個係數並將它們相加,我們可以從上述方程式中匯出一個公式或使用每個數字的階乘函式來獲得一個和,所以我們將使用階乘函式,在其中我們將傳遞特定方程式的 n 和 r,然後將它們相加,然後我們得到解決方案
範例
#include <iostream> using namespace std; int fact(int n){ int fact = 1, i; for (i = 2; i <= n; i++){ fact *= i; } return fact; } int main(){ int n=5; int sum = 0; int temp=0; for (int r = 0; r <= n; r++){ temp = fact(n)/(fact(r)*fact(n-r)); sum +=(temp*temp); } cout<<sum; return 0; }
輸出
252
廣告