题目
设计函数求一元多项式的导数。(注: x n x^n x ? n ? ? ( n n n 为整数)的一阶导数为 n x n ? 1 n x^{n-1} n x ? n ? 1 ? ? 。)
输入格式:
以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过 1000 的整数)。数字间以空格分隔。
输出格式:
以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是 0,但是表示为 0 0
。
输入样例:
3 4 -5 2 6 1 -2 0
输出样例:
12 3 -10 1 6 0
代码
#include<iostream>
#include<string>
using namespace std;
class node
{
public:
int a;
int n;
node* next;
node(int i, int j, node* p = NULL)
{
a = i;
n = j;
next = p;
}
string doit()
{
if (n != 0)
return to_string(a * n) + " " + to_string((n - 1));
else
return "";
}
};
int main()
{
node* p = NULL, * head = NULL;
int a, n;
string output = "";
while (1)
{
cin >> a >> n;
if (head == NULL)
{
p = new node(a,n);
head = p;
}
else
{
p->next = new node(a,n);
p = p->next;
}
if (getchar() == 10)
break;
}
for (p = head; p != NULL; p = p->next)
{
if(p->doit()!="")
output += p->doit() + " ";
}
if(output!="")
cout << output.substr(0,output.length()-1);
else
cout<<"0 0";
return 0;
}