PTA_乙级_1010 一元多项式求导(C++_模拟)

设计函数求一元多项式的导数。(注: x n x_n (n为整数)的一阶导数为 n x n 1 nx^{n−1} 。)

输入格式:

以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过 1000 的整数)。数字间以空格分隔。

输出格式:

以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是 0,但是表示为 0 0。

输入样例:

3 4 -5 2 6 1 -2 0

输出样例:

12 3 -10 1 6 0

源码

#include<bits/stdc++.h>
using namespace std;
class node
{
public:
    int x, z;
}s[10000];
int cnt;
int main()
{
    int a, b;
    int flag = -1, num = 1;
    while (cin >> a >> b)
    {
        s[cnt].x = a;
        s[cnt++].z = b;
    }
    if (!s[0].z)
    {
        cout << "0 0";
        return 0;
    }
    for (int i = 0; i < cnt; i++)
        if (s[i].z)
        {
            if (i == cnt - 1 || (i != cnt && s[i + 1].z == 0))
                cout << s[i].x * s[i].z << " " << s[i].z - 1;
            else
                cout << s[i].x * s[i].z << " " << s[i].z - 1<<" ";
        }
    return 0;
}
发布了228 篇原创文章 · 获赞 30 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43510916/article/details/104337326
今日推荐