PAT Grade B 1010 Unary Polynomial Derivation (25 points)

topic content

Design a function to take the derivative of a polynomial in one variable.

Input format:

Enter the polynomial nonzero coefficients and exponents in exponentially decreasing fashion (both absolute values ​​are integers up to 1000). The numbers are separated by spaces.

Output format:

Outputs the coefficients and exponents of the nonzero terms of the derivative polynomial in the same format as the input. Numbers are separated by spaces, but no extra spaces at the end. Note that the "zero polynomial" has both exponents and coefficients 0, but is represented as  0 0.

Input sample:

3 4 -5 2 6 1 -2 0

no blank line at the end

Sample output:

12 3 -10 1 6 0

no blank line at the end

Problem solving ideas

Process each term of the polynomial in turn , input a set of coefficients and exponents, and output a result, note that if the first one is a zero polynomial, print 0 0

Detailed code

#include <iostream>
#include <string>
#include <algorithm> 
#include <cmath>
using namespace std;
int main()
{
	int a=0, n=0;
	cin >> a >> n;
	if (n!=0)
		cout << a*n <<' ' << n-1;
	else cout <<"0 0";  //如果首项是零多项式的话 需要输出 0 0
	while (cin >> a >> n)
	if (n!=0)
		cout <<' ' << a*n <<' ' << n-1;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45660485/article/details/119274178