1010 一元多项式求导 (25 分)
设计函数求一元多项式的导数。(注:xn(n为整数)的一阶导数为nxn−1。)
输入格式:
以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过 1000 的整数)。数字间以空格分隔。
输出格式:
以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是 0,但是表示为 0 0。
输入样例:
3 4 -5 2 6 1 -2 0
输出样例:
12 3 -10 1 6 0思路: 这个题目非常简单但是容易出错: 1、当所有多项式都是零的话,输出“0 0” 2、输出的时候不能使用i==nodes.size()-1的方式控制空格的输出,因为无法得知哪一个才是最后一个才能输出的。很有可能只有第一个需要输出,而后面全是0
#include<iostream>
#include<string>
#include<vector>
#include<string>
#include<cstdio>
#include<cmath>
#include<string.h>
#include<algorithm>
#include<map>
#include<stack>
using namespace std;
struct Node
{
int exp;
int co;
};
int main()
{
vector<Node> nodes;
Node temp;
while(cin>>temp.co>>temp.exp)
nodes.push_back(temp);
bool flag=true;
for(int i=0; i<nodes.size(); i++)
{
Node temp=nodes[i];
if(i==0)
{
if(temp.exp==0)
continue;
else
{
flag=false;
cout<<temp.co*temp.exp<<" "<<temp.exp-1;
}
}
else
{
if(temp.exp==0)
continue;
else
{
flag=false;
cout<<" "<<temp.co*temp.exp<<" "<<temp.exp-1;
}
}
}
if(flag)
cout<<"0 0";
return 0;
}
来源:https://www.cnblogs.com/zhanghaijie/p/10400693.html