PAT 1010 一元多项式求导

192 阅读1分钟

题目链接
题目就不写了,链接里都有,直接进入正题。

  1. 要注意这个“零多项式”,这个是指输入是0 0的情况还是指输入为空格的情况我没太理解。
  2. 这个题目必须要有输出,比较极端的情况就是输入的是0 0或者什么都不输入,输出是0 0。
  3. 将2中的两种情况排除剩下的就是根据正常的输出按求导法则求出输出。
#include <iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;

int main(){
	string buff;
	getline(cin, buff);
	int a, b;
	vector<int> res;//用来保存最后输出的结果
	istringstream n(buff);
	while (n >> a >> b) {
		if (a == 0 && b == 0) {
			cout << "0 0" << endl;//零多项式情况
			return 0;
		}
		else {
			if (b != 0) {
				res.push_back(a*b);
				res.push_back(b - 1);
			}		
		}
	}
	if (res.size() == 0)//如果输入是空,res则为空,必须输出0 0
	{
		cout << "0 0" << endl;
		return 0;
	}
	cout << res[0];
	for (int i = 1; i < res.size(); i++) {
		cout << " " << res[i];
	}
	cout << endl;
	return 0;
}