数据结构与算法题目集(中文)7-16 一元多项式求导 (20分)

1.题目

设计函数求一元多项式的导数。

输入格式:

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

输出格式:

以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。

输入样例:

3 4 -5 2 6 1 -2 0

输出样例:

12 3 -10 1 6 0

2.题目分析

见PTAhttps://pintia.cn/problem-sets/15/problems/710该题,

分析: https://blog.csdn.net/qq_42325947/article/details/104170156

3.代码

#include<iostream>
#include<cstdio>
using namespace std;
int a[10001] = { 0 };
int b[10001] = { 0 };
int c[10001] = { 0 };
int d[10001] = { 0 };
int main()
{
	int x, z;//x为系数,z为指数
	while (cin>>x>>z)
	{
		a[z] += x;
	}
	
	for (int i = 1; i < 10001; i++)
	{
		if (a[i])
		{
			c[i-1] = i*a[i];//从1开始向前赋值
		}
	}
	int cn = 0;
	for (int i = 10000; i >= 0; i--)//倒着输出
	{
		if (c[i])
		{
			if (cn == 0)
			{
				cout << c[i] << " " << i; cn++;
			}
			else
				cout << " " << c[i] << " " << i;
		}
	}
	if (cn == 0)cout << "0 0";

	

}
原文地址:https://www.cnblogs.com/Jason66661010/p/12789019.html