Derivation of one-element polynomial

Source: Internet
Author: User

/* Unary polynomial derivation (25) Design function to find the derivative of a polynomial. Input format: input polynomial non-0 coefficients and exponents in exponential degradation (absolute values are integers not exceeding 1000). The numbers are separated by a space. Output format: Outputs the coefficients and exponents of the derivative polynomial not 0 in the same format as the input. The numbers are separated by a space, but cannot have extra spaces at the end. Note that the exponent and coefficients for the "0 polynomial" are 0, but are expressed as "0 0".  Input Sample: 3 4-5 2 6 1-2 0 i.e. 3x^4-5x^2 + 6x-2 Output Sample: 12 3-10 1 6 0 i.e. 12x^3-10x + 6-0 derivative: The derivative of ax^n is a*nx^ (n-1), which is the output in the case The derivative of the A*n n-1 constant is 0*/#pragma once#include<vector> #include <sstream> #include <iostream> #include <    string>using namespace std;struct item{int e;  int n;    };void Input (Vector<item>&vec) {string line;    Item it;    int num;    bool BN = true;    Getline (CIN, line, ' \ n ');    Istringstream stream (line);        while (stream >> num) {static int i = 0;        if (bN) IT.N = num;        else IT.E = num;        if (++i%2==0) vec.push_back (IT);    BN =!bn;    }}//merge similar top void comb (Vector<item>&a, vector<item>&b) {int size = A.size ();    int i;  for (i = 0; i < size-1; ++i) {if (A[I].E = = A[i + 1].e)      {A[I+1].N + = A[I].N;        } else {if (A[I].N! = 0) B.push_back (a[i]); }}//The last missing if (A[I].N! = 0) B.push_back (A[i]);}    void Output (VECTOR&LT;ITEM&GT;&AMP;VEC) {int size = Vec.size (); if (size = = 1 && vec[0].e = = 0 && vec[0].n==0) | | size = = 0) {cout << "0 0" << end        L    Return        } for (int i = 0; i < size; ++i) {if (vec[i].e = = 0) cout << ' 0 ';        else if (vec[i].e-1 = = 0) cout << VEC[I].N;            else {cout << VEC[I].N * vec[i].e << ';        cout << vec[i].e-1;    } if (i! = size-1) cout << '; } cout << Endl;}    void Run () {Vector<item>vec, der;    Input (VEC);    Comb (Vec, der); Output (der);}    int main (void) {Run (); return 0;}

Derivation of one-element polynomial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.