Dada la string polinomial str , la tarea es diferenciar la string dada e imprimir la string después de diferenciarla.
Nota: El formato de entrada es tal que hay un espacio en blanco entre un término y el símbolo ‘+’, ‘-‘
Ejemplos:
Entrada: str = “4X 3 + 3X 1 + 2X 2 ”
Salida: “12X 2 + 3X 0 + 4X 1 ”
Explicación:
La derivada de p(x) = A*X N es p'(x) = A * N * X N – 1
Entrada: str = “5X 4 + 6X 2 + 5X 2 ”
Salida: “20X 3 + 12X 1 + 10X 1 ”
Enfoque: La idea es observar que cuando la ecuación dada consta de múltiples polinomios
, la diferenciación del polinomio dado
. Y, se sabe que la derivada de
es
Por lo tanto, dividimos la string dada y diferenciamos cada término en ella.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to differentiate the // given polynomial #include "bits/stdc++.h" #define MOD (1e9 + 7); using ll = int64_t; using ull = uint64_t; #define ll long long using namespace std; // Function to differentiate the // given term string diffTerm(string pTerm) { // Get the coefficient string coeffStr = "", S = ""; int i; // Loop to get the coefficient for (i = 0; pTerm[i] != 'x'; i++) coeffStr.push_back(pTerm[i]); long long coeff = atol(coeffStr.c_str()); // Loop to get the power of each term string powStr = ""; for (i = i + 2; i != pTerm.size(); i++) powStr.push_back(pTerm[i]); long long power = atol(powStr.c_str()); string a, b; // Converting the value // to the string ostringstream str1, str2; // For ax^n, we find (n)*a*x^(n-1) coeff = coeff * power; str1 << coeff; a = str1.str(); power--; str2 << power; b = str2.str(); S += a + "X^" + b; return S; } // Function to differentiate the // given polynomial string diffstr(string& poly) { // We use istringstream to get // the input in tokens istringstream is(poly); string pTerm, S = ""; // For every token, compute the // differentiation while (is >> pTerm) { // If the token is equal to // '+', '-' then // continue with the string if (pTerm == "+") { S += " + "; continue; } if (pTerm == "-") { S += " - "; continue; } // Otherwise find the differentiation // of that particular term else S += diffTerm(pTerm); } return S; } // Driver code int main() { string str = "5x^4 + 6x^2 + 5x^2"; cout << diffstr(str); return 0; }
Python3
# Python3 program to differentiate # the given polynomial MOD = (1e9 + 7) # Function to differentiate # the given term def diffTerm(pTerm): # Get the coefficient coeffStr = "" S = "" # Loop to get the # coefficient i = 0 while (i < len(pTerm) and pTerm[i] != 'x'): coeffStr += (pTerm[i]) i += 1 coeff = int(coeffStr) # Loop to get the power # of each term powStr = "" j = i + 2 while j < len(pTerm): powStr += (pTerm[j]) j += 1 power = int(powStr) # For ax^n, we find # (n)*a*x^(n-1) coeff = coeff * power a = str(coeff) power -= 1 b = str(power) S += a + "X^" + b return S # Function to differentiate # the given polynomial def diffstr(poly): pTerm = poly.split(" ") S = "" for i in range(len(pTerm)): # If the token is equal to # '+', '-' then # continue with the string if (pTerm[i] == "+"): S += " + " continue if (pTerm[i] == "-"): S += " - " continue # Otherwise find the differentiation # of that particular term else: S += diffTerm(pTerm[i]) return S # Driver code if __name__ == "__main__": st = "5x^4 + 6x^2 + 5x^2" print(diffstr(st)) # This code is contributed by Chitranayal
20X^3 + 12X^1 + 10X^1
Publicación traducida automáticamente
Artículo escrito por RishavSinghMehta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA