📜  给定值在多项式中的积分

📅  最后修改于: 2021-04-29 13:13:57             🧑  作者: Mango

给定代表多项式和数字N的字符串str ,任务是相对于X将该多项式以给定值N积分。
例子:

方法:想法是使用集成的身份。对于某些具有N的幂的给定函数X,该项的积分由下式给出:

\int (X^{N}) = \frac{X^{N + 1}}{N + 1}

因此,请按照以下步骤计算答案:

  • 获取字符串。
  • 拆分字符串并根据上述公式执行集成。
  • 将N的值替换为所获得的表达式中的N。
  • 将所有单个值相加即可得出最终的整数值。

下面是上述方法的实现:

C++
// C++ program to find the integration
// of the given polynomial for the
// value N
 
#include 
using namespace std;
typedef long long ll;
 
// Function to return the integral
// of the given term
double inteTerm(string pTerm, ll val)
{
    // Get the coefficient
    string coeffStr = "";
    int i;
 
    // Loop to iterate through the string
    // and get the coefficient
    for (i = 0; pTerm[i] != 'x'; i++)
        coeffStr.push_back(pTerm[i]);
    ll coeff = atol(coeffStr.c_str());
 
    // Get the Power
    string powStr = "";
 
    // Loop to skip 2 characters for x and ^
    for (i = i + 2; i != pTerm.size(); i++)
        powStr.push_back(pTerm[i]);
    ll power = atol(powStr.c_str());
 
    // Return the computed integral
    return (coeff * pow(val, power + 1))
           / (power + 1);
}
 
// Functionto find the integration
// of the given polynomial for the
// value N
double integrationVal(string poly, int val)
{
    ll ans = 0;
 
    // Using string stream to get the
    // input in tokens
    istringstream is(poly);
    string pTerm;
 
    while (is >> pTerm) {
 
        // If the token is equal to '+' then
        // continue with the string
        if (pTerm == "+")
            continue;
 
        // Otherwise find the integration
        // of that particular term
        else
            ans = (ans + inteTerm(pTerm, val));
    }
    return ans;
}
 
// Driver code
int main()
{
    string str = "4x^3 + 3x^1 + 2x^2";
    int val = 2;
    cout << integrationVal(str, val);
    return 0;
}


Java
// Java program for the above approach
public class GFG
{
 
  // Function to return the integral
  // of the given term
  static int inteTerm(String pTerm, int val)
  {
 
    // Get the coefficient
    String coeffStr = "";
 
    // Loop to iterate through
    // the string and get the
    // coefficient
    int i = 0;
    while (i < pTerm.length() && pTerm.charAt(i) != 'x')
    {
      coeffStr += pTerm.charAt(i);
      i += 1;
    }
 
    int coeff = Integer.parseInt(coeffStr);
 
    // Get the Power
    String powStr = "";
 
    // Loop to skip 2 characters
    // for x and ^
    int j = i + 2;
    while(j< pTerm.length())
    {
      powStr += (pTerm.charAt(j));
      j += 1;
    }
    int power = Integer.parseInt(powStr);
 
    // Return the computed integral
    return ((coeff * (int)Math.pow(val, power + 1)) / (power + 1));
  }
 
  // Functionto find the integration
  // of the given polynomial for the
  // value N
  static int integrationVal(String poly, int val)
  {
    int ans = 0;
 
    // Using string stream to
    // get the input in tokens
    String[] stSplit = poly.split(" \\+ ");
 
    int i = 0;
    while(i < stSplit.length)
    {
      ans = (ans + inteTerm(stSplit[i], val));
      i += 1;
    }
 
    return ans;
  }
 
  // Driver code
  public static void main(String[] args) {
    String st = "4x^3 + 3x^1 + 2x^2";
    int val = 2;
    System.out.println(integrationVal(st, val));
  }
}
 
// This code is contributed by divyesh072019.


Python3
# Python3 program to find
# the integration of the
# given polynomial for the
# value N
 
# Function to return the integral
# of the given term
def inteTerm(pTerm, val):
 
    # Get the coefficient
    coeffStr = ""
 
    # Loop to iterate through
    # the string and get the
    # coefficient
    i = 0
    while (i < len(pTerm) and
           pTerm[i] != 'x'):
        coeffStr += pTerm[i]
        i += 1
         
    coeff = int(coeffStr)
 
    # Get the Power
    powStr = ""
 
    # Loop to skip 2 characters
    # for x and ^
    j = i + 2
    while j< len(pTerm):
        powStr += (pTerm[j])
        j += 1
    power = int(powStr)
 
    # Return the computed integral
    return ((coeff *
             pow(val,
                 power + 1)) //
            (power + 1))
 
# Functionto find the integration
# of the given polynomial for the
# value N
def integrationVal(poly, val):
 
    ans = 0
 
    # Using string stream to
    # get the input in tokens
    stSplit = poly.split("+")
 
    i = 0
    while i < len(stSplit):
        ans = (ans +
               inteTerm(stSplit[i],
                        val))
        i += 1
 
    return ans
 
# Driver code
if __name__ == "__main__":
 
    st = "4x^3 + 3x^1 + 2x^2"
    val = 2
    print(integrationVal(st, val))
 
# This code is contributed by Chitranayal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
 
  // Function to return the integral
  // of the given term
  static int inteTerm(string pTerm, int val)
  {
 
    // Get the coefficient
    string coeffStr = "";
 
    // Loop to iterate through
    // the string and get the
    // coefficient
    int i = 0;
    while (i < pTerm.Length && pTerm[i] != 'x')
    {
      coeffStr += pTerm[i];
      i += 1;
    }
 
    int coeff = Convert.ToInt32(coeffStr);
 
    // Get the Power
    string powStr = "";
 
    // Loop to skip 2 characters
    // for x and ^
    int j = i + 2;
    while(j< pTerm.Length)
    {
      powStr += (pTerm[j]);
      j += 1;
    }
    int power = Convert.ToInt32(powStr);
 
    // Return the computed integral
    return ((coeff * (int)Math.Pow(val, power + 1)) / (power + 1));
  }
 
  // Functionto find the integration
  // of the given polynomial for the
  // value N
  static int integrationVal(string poly, int val)
  {
    int ans = 0;
 
    // Using string stream to
    // get the input in tokens
    string[] stSplit = poly.Split('+');
 
    int i = 0;
    while(i < stSplit.Length)
    {
      ans = (ans + inteTerm(stSplit[i], val));
      i += 1;
    }
 
    return ans;
  }
 
  // Driver code
  static void Main() {
    string st = "4x^3 + 3x^1 + 2x^2";
    int val = 2;
    Console.WriteLine(integrationVal(st, val));
  }
}
 
// This code is contributed by divyeshrabadiya07.


输出:
27