1010 derivation of unary polynomial
Design function to find the derivative of unary polynomial. (note: the first derivative of x ^ n (n is an integer) is nx ^ n (n-1).)
input format:
enter polynomial non-zero coefficients and exponents in an exponentially descending manner (the absolute values are integers not exceeding 1000). Numbers are separated by spaces.
output format:
outputs the coefficients and exponents of derivative polynomials in the same format as the input. Numbers are separated by spaces, but there must be no extra spaces at the end. Notice that the exponent and coefficient of the zero polynomial are both 0, but expressed as 0.
input sample:
3 4-52 61-20
output sample:
12 3-10 1 60
my code:
-sharpinclude<iostream>
-sharpinclude<vector>
using namespace std;
int main()
{
vector<int> v;//
vector<int> result;//
int n;
int a, b;//
while(cin>>n)
{
v.push_back(n);
if (cin.get() == "\n")
break;
}
for(int i = 0; i < v.size(); i+=2)
{
if(v[i+1] == 0)
continue;
a = v[i] * v[i+1];
b = v[i+1] - 1;
result.push_back(a);
result.push_back(b);
}
for(int i = 0; i < result.size() - 1; iPP)
{
cout<<result[i]<<" ";
}
cout<<result[result.size() - 1];
return 0;
}
the results are all correct, but there is an error when submitting the code. What is the reason?
(currently all we know about segment errors is the memory space that the program should not access)