给定三个整数A、X和n。任务是在二项式展开序列中找到中间项。 例如:
Input : A = 1, X = 1, n = 6Output : MiddleTerm = 20Input : A = 2, X = 4, n = 7Output : MiddleTerm1 = 35840, MiddleTerm2 = 71680
方法
(A+X) N = N C 0 A. N 十、 0 + N C 1. A. n-1 十、 1. + N C 2. A. n-2 十、 2. + ……… + N C n-1 A. 1. 十、 n-1 + N C N A. 0 十、 N (A+X)的二项展开式中的项总数 N 是(n+1)。 二项式展开式中的一般项由下式给出: T r+1 = N C R A. n-r 十、 R 如果n是偶数: 设m为二项式展开级数的中间项 n=2m m=n/2 我们知道会有n+1项, n+1=2m+1 在这种情况下,只有一个中期选举。这个中期是(m+1) th 学期 因此,中期 T m+1 = N C M A. n-m 十、 M 如果n是奇数: 设m为二项式展开级数的中间项 设n=2m+1 m=(n-1)/2 术语数量=n+1=2m+1+1=2m+2 在这种情况下,将有两个中间条款。这些中间术语将是(m+1) th 和(m+2) th 学期 因此,中间术语是: T m+1 = N C (n-1)/2 A. (n+1)/2 十、 (n-1)/2 T m+2 = N C (n+1)/2 A. (n-1)/2 十、 (n+1)/2
C++
// C++ program to find the middle term // in binomial expansion series. #include <bits/stdc++.h> using namespace std; // function to calculate // factorial of a number int factorial( int n) { int fact = 1; for ( int i = 1; i <= n; i++) fact *= i; return fact; } // Function to find middle term in // binomial expansion series. void findMiddleTerm( int A, int X, int n) { int i, j, aPow, xPow; float middleTerm1, middleTerm2; if (n % 2 == 0) { // If n is even // calculating the middle term i = n / 2; // calculating the value of A to // the power k and X to the power k aPow = ( int ) pow (A, n - i); xPow = ( int ) pow (X, i); middleTerm1 = (( float )factorial(n) / (factorial(n - i) * factorial(i))) * aPow * xPow; cout << "MiddleTerm = " << middleTerm1 << endl; } else { // If n is odd // calculating the middle term i = (n - 1) / 2; j = (n + 1) / 2; // calculating the value of A to the // power k and X to the power k aPow = ( int ) pow (A, n - i); xPow = ( int ) pow (X, i); middleTerm1 = (( float )factorial(n) / (factorial(n - i) * factorial(i))) * aPow * xPow; // calculating the value of A to the // power k and X to the power k aPow = ( int ) pow (A, n - j); xPow = ( int ) pow (X, j); middleTerm2 = (( float )factorial(n) / (factorial(n - j) * factorial(j))) * aPow * xPow; cout << "MiddleTerm1 = " << middleTerm1 << endl; cout << "MiddleTerm2 = " << middleTerm2 << endl; } } // Driver code int main() { int n = 5, A = 2, X = 3; // function call findMiddleTerm(A, X, n); return 0; } |
JAVA
// Java program to find the middle term // in binomial expansion series. import java.math.*; class GFG { // function to calculate factorial // of a number static int factorial( int n) { int fact = 1 , i; if (n == 0 ) return 1 ; for (i = 1 ; i <= n; i++) fact *= i; return fact; } // Function to find middle term in // binomial expansion series. static void findmiddle( int A, int X, int n) { int i, j, aPow, xPow; float middleTerm1, middleTerm2; if (n % 2 == 0 ) { // If n is even // calculating the middle term i = n / 2 ; // calculating the value of A to // the power k and X to the power k aPow = ( int )Math.pow(A, n - i); xPow = ( int )Math.pow(X, i); middleTerm1 = (( float )factorial(n) / (factorial(n - i) * factorial(i))) * aPow * xPow; System.out.println( "MiddleTerm = " + middleTerm1); } else { // If n is odd // calculating the middle term i = (n - 1 ) / 2 ; j = (n + 1 ) / 2 ; // calculating the value of A to the // power k and X to the power k aPow = ( int )Math.pow(A, n - i); xPow = ( int )Math.pow(X, i); middleTerm1 = (( float )factorial(n) / (factorial(n - i) * factorial(i))) * aPow * xPow; // calculating the value of A to the // power k and X to the power k aPow = ( int )Math.pow(A, n - j); xPow = ( int )Math.pow(X, j); middleTerm2 = (( float )factorial(n) / (factorial(n - j) * factorial(j))) * aPow * xPow; System.out.println( "MiddleTerm1 = " + middleTerm1); System.out.println( "MiddleTerm2 = " + middleTerm2); } } // Driver code public static void main(String[] args) { int n = 6 , A = 2 , X = 4 ; // calling the function findmiddle(A, X, n); } } |
Python3
# Python3 program to find the middle term # in binomial expansion series. import math # function to calculate # factorial of a number def factorial(n) : fact = 1 for i in range ( 1 , n + 1 ) : fact = fact * i return fact; # Function to find middle term in # binomial expansion series. def findMiddleTerm(A, X, n) : if (n % 2 = = 0 ) : # If n is even # calculating the middle term i = int (n / 2 ) # calculating the value of A to # the power k and X to the power k aPow = int (math. pow (A, n - i)) xPow = int (math. pow (X, i)) middleTerm1 = ((math.factorial(n) / (math.factorial(n - i) * math.factorial(i))) * aPow * xPow) print ( "MiddleTerm = {}" . format (middleTerm1)) else : # If n is odd # calculating the middle term i = int ((n - 1 ) / 2 ) j = int ((n + 1 ) / 2 ) # calculating the value of A to the # power k and X to the power k aPow = int (math. pow (A, n - i)) xPow = int (math. pow (X, i)) middleTerm1 = ((math.factorial(n) / (math.factorial(n - i) * math.factorial(i))) * aPow * xPow) # calculating the value of A to the # power k and X to the power k aPow = int (math. pow (A, n - j)) xPow = int (math. pow (X, j)) middleTerm2 = ((math.factorial(n) / (math.factorial(n - j) * math.factorial(j))) * aPow * xPow) print ( "MiddleTerm1 = {}" . format ( int (middleTerm1))) print ( "MiddleTerm2 = {}" . format ( int (middleTerm2))) # Driver code n = 5 A = 2 X = 3 # function call findMiddleTerm(A, X, n) # This code is contributed by # manishshaw1. |
C#
// C# program to find the middle term // in binomial expansion series. using System; class GFG { // function to calculate factorial // of a number static int factorial( int n) { int fact = 1, i; if (n == 0) return 1; for (i = 1; i <= n; i++) fact *= i; return fact; } // Function to find middle term in // binomial expansion series. static void findmiddle( int A, int X, int n) { int i, j, aPow, xPow; float middleTerm1, middleTerm2; if (n % 2 == 0) { // If n is even // calculating the middle term i = n / 2; // calculating the value of A to // the power k and X to the power k aPow = ( int )Math.Pow(A, n - i); xPow = ( int )Math.Pow(X, i); middleTerm1 = (( float )factorial(n) / (factorial(n - i) * factorial(i))) * aPow * xPow; Console.WriteLine( "MiddleTerm = " + middleTerm1); } else { // If n is odd // calculating the middle term i = (n - 1) / 2; j = (n + 1) / 2; // calculating the value of A to the // power k and X to the power k aPow = ( int )Math.Pow(A, n - i); xPow = ( int )Math.Pow(X, i); middleTerm1 = (( float )factorial(n) / (factorial(n - i) * factorial(i))) * aPow * xPow; // calculating the value of A to the // power k and X to the power k aPow = ( int )Math.Pow(A, n - j); xPow = ( int )Math.Pow(X, j); middleTerm2 = (( float )factorial(n) / (factorial(n - j) * factorial(j))) * aPow * xPow; Console.WriteLine( "MiddleTerm1 = " + middleTerm1); Console.WriteLine( "MiddleTerm2 = " + middleTerm2); } } // Driver code public static void Main() { int n = 5, A = 2, X = 3; // calling the function findmiddle(A, X, n); } } // This code is contributed by anuj_67. |
PHP
<?php // PHP program to find the middle term // in binomial expansion series. // function to calculate // factorial of a number function factorial(int $n ) { $fact = 1; for ( $i = 1; $i <= $n ; $i ++) $fact *= $i ; return $fact ; } // Function to find middle term in // binomial expansion series. function findMiddleTerm( $A , $X , $n ) { $i ; $j ; $aPow ; $xPow ; $middleTerm1 ; $middleTerm2 ; if ( $n % 2 == 0) { // If n is even // calculating the middle term $i = $n / 2; // calculating the value of A to // the power k and X to the power k $aPow = pow( $A , $n - i); $xPow = pow( $X , $i ); $middleTerm1 = $factorial ( $n ) / (factorial( $n - $i ) * factorial( $i )) * $aPow * $xPow ; echo "MiddleTerm = " , "" , $middleTerm1 ; } else { // If n is odd // calculating the middle term $i = ( $n - 1) / 2; $j = ( $n + 1) / 2; // calculating the value of A to the // power k and X to the power k $aPow = pow( $A , $n - $i ); $xPow = pow( $X , $i ); $middleTerm1 = ((float)factorial( $n ) / (factorial( $n - $i ) * factorial( $i ))) * $aPow * $xPow ; // calculating the value of A to the // power k and X to the power k $aPow = pow( $A , $n - $j ); $xPow = pow( $X , $j ); $middleTerm2 = factorial( $n ) / (factorial( $n - $j ) * factorial( $j )) * $aPow * $xPow ; echo "MiddleTerm1 = " , $middleTerm1 , "" ; echo "MiddleTerm2 = " , $middleTerm2 ; } } // Driver code $n = 5; $A = 2; $X = 3; // function call findMiddleTerm( $A , $X , $n ); // This code is contributed by Vishal Tripathi. ?> |
Javascript
<script> // JavaScript program to find the middle term // in binomial expansion series. // function to calculate // factorial of a number function factorial(n) { let fact = 1; for (let i = 1; i <= n; i++) fact *= i; return fact; } // Function to find middle term in // binomial expansion series. function findMiddleTerm(A, X, n) { let i, j, aPow, xPow; let middleTerm1, middleTerm2; if (n % 2 == 0) { // If n is even // calculating the middle term i = Math.floor(n / 2); // calculating the value of A to // the power k and X to the power k aPow = Math.floor(Math.pow(A, n - i)); xPow = Math.floor(Math.pow(X, i)); middleTerm1 = Math.floor(factorial(n) / (factorial(n - i) * factorial(i))) * aPow * xPow; document.write( "MiddleTerm = " + middleTerm1 + "<br>" ); } else { // If n is odd // calculating the middle term i = Math.floor((n - 1) / 2); j = Math.floor((n + 1) / 2); // calculating the value of A to the // power k and X to the power k aPow = Math.floor(Math.pow(A, n - i)); xPow = Math.floor(Math.pow(X, i)); middleTerm1 = Math.floor(factorial(n) / (factorial(n - i) * factorial(i))) * aPow * xPow; // calculating the value of A to the // power k and X to the power k aPow = Math.floor(Math.pow(A, n - j)); xPow = Math.floor(Math.pow(X, j)); middleTerm2 = Math.floor(factorial(n) / (factorial(n - j) * factorial(j))) * aPow * xPow; document.write( "MiddleTerm1 = " + middleTerm1 + "<br>" ); document.write( "MiddleTerm2 = " + middleTerm2 + "<br>" ); } } // Driver code let n = 5, A = 2, X = 3; // function call findMiddleTerm(A, X, n); // This code is contributed by Surbhi Tyagi. </script> |
MiddleTerm1 = 720MiddleTerm2 = 1080