原题: https://pintia.cn/problem-sets/994805260223102976/problems/994805313708867584
设计函数求一元多项式的导数。
(注:
x
n
x^{n}
xn(n为整数)的一阶导数为
n
x
n
−
1
nx^{n-1}
nxn−1。)
输入格式:
以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过 1000 的整数)。数字间以空格分隔。
输出格式:
以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是 0,但是表示为 0 0。
输入样例:
3 4 -5 2 6 1 -2 0
输出样例:
12 3 -10 1 6 0
注意:此题的注意点为常数多项式的导数为0,要输出"0 0"。
例如单独的3 0要输出0 0
代码:
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main() {
int num1,num2;
int start = 1;
while (cin >> num1 >> num2) {
if (start && num2 == 0) {
cout << "0 0"; //导数为零和多项式
break;
}
if (num2 == 0) break; //到了末尾
if (start == 0) cout << " ";
cout << num1*num2<<" ";
cout << num2 - 1;
start = 0; //不是开头
}
cout << endl;
system("pause");
return 0;
}