[PAT B] 1010 一元多项式求导

1010 一元多项式求导 --没 AC

题目

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

输入格式:

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

输出格式:

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

输入样例:

3 4 -5 2 6 1 -2 0

输出样例:

12 3 -10 1 6 0

思路分析 --没 AC

读一组,算一组,出一组,不用全存下来再处理
注意特殊情况的处理

代码

#include <iostream>

using namespace std;
struct Poly {
    int coefficient;
    int index;
};
Poly poly;
int main() {
        while (1) {
            cin >> poly.coefficient;
            cin >> poly.index;
            if (poly.index == 0||poly.coefficient==0);
            else {
                poly.coefficient = poly.coefficient * poly.index;
                poly.index = poly.index - 1;
                if (poly.index == 0) cout << poly.coefficient << " " << poly.index;
                else cout << poly.coefficient << " " << poly.index << " ";
            }
            if (cin.get() == '\n') break;

        }

    return 0;
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
一只热爱编程的松鼠
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!