• [PTA] PAT(A) 1009 Product of Polynomials (25 分)


    Problem

    portal:1009 Product of Polynomials

    Description

    This time, you are supposed to find $A×B$ where $A$ and $B$ are two polynomials.

    ## Input

    Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: $K$ $N_{1}$ $a_{N1}$ $N_{2}$ $a_{N2}$ $...$ $N_{K}$ $a_{NK}$ where $K$ is the number of nonzero terms in the polynomial, $N_{i}$ and $a_{Ni}$($i = 1, 2, ... , K$) are the exponents and coefficients, respectively. It is given that $ 1 leq K leq 10$, $0 leq N_{K} < ... < N_{2} < N_{1} leq 1000$.

    ## Output

    For each test case you should output the product of $A$ and $B$ in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

    ## Sample ### Sample Input ```cpp 2 1 2.4 0 3.2 2 2 1.5 1 0.5 ``` ### Sample Output ```cpp 3 3 3.6 2 6.0 1 1.6 ``` # Solution ## Analysis 题目要求是求两个多项式的乘积,并按照输入的格式进行输出。 读入两个多项式,相乘,输出结果,就是程序的整体流程。 首先,是多项式的存储问题。 使用数组(vector)将多项式的每一项存储起来,每一项是一个结构体,包括两个参数,指数和系数。 接下来就是计算。 第一个多项式的每一项与第二个多项式的每一项相乘,结果存入临时多项式中,如果项相乘得到的指数有相同的,那么将系数相加,否则正常处理。 在存储的时候,多项式的每一项存储顺序是指数大的在前,所以在后面的相乘中,只有在第一个多项式切换到下一项时,才会将`pos`的值重新指到最前面,因为第一个多项式的这一项与第二个多项式中的每一项后面的相乘的结果的指数肯定小于前面的,所以只需要比较后面项的指数大小和当前项指数大小即可。 最后将其中系数为零的项删除掉。

    Code

    #include <bits/stdc++.h>
    using namespace std;
    
    struct item {
    	double coefficient;
    	int exponent;
    
    	item operator*(item it) {
    		item it_result;
    		it_result.coefficient = coefficient * it.coefficient;
    		it_result.exponent = exponent + it.exponent;
    		return it_result;
    	}
    };
    
    struct polynomials {
    	vector<item> items;
    	polynomials operator*(polynomials &po) {
    		polynomials po_result;
    		item it_result;
    		int pos = 0;
    		for (int i = 0; i < items.size(); i++) {
    			pos = 0;
    			for (int j = 0; j < po.items.size(); j++) {
    				it_result = items[i] * po.items[j];
    				while (pos < po_result.items.size() && po_result.items[pos].exponent > it_result.exponent)
    					pos++;
    				if (pos >= po_result.items.size()) {
    				    po_result.items.push_back(it_result);
    				} else if (po_result.items[pos].exponent == it_result.exponent) {
    					po_result.items[pos].coefficient += it_result.coefficient;
    				} else {
    					po_result.items.insert(po_result.items.begin() + pos, it_result);
    				}
    			}
    		}
    		for (int i = 0; i < po_result.items.size(); i++) {
    			if (po_result.items[i].coefficient == 0) {
    				po_result.items.erase(po_result.items.begin() + i);
    				i--;
    			}
    		}
    		return po_result;
    	}
    };
    
    istream& operator>>(istream &is, polynomials &po) {
    	po.items.clear();
    	int n;
    	item it;
    	is >> n;
    	for (int i = 0; i < n; i++) {
    		is >> it.exponent >> it.coefficient;
    		po.items.push_back(it);
    	}
    	return is;
    }
    
    ostream& operator<<(ostream &os, polynomials &po) {
    	os << po.items.size();
    	for (int i = 0; i < po.items.size(); i++) {
    		os << " " << po.items[i].exponent;
    		os << fixed << setprecision(1) << " " << po.items[i].coefficient;
    	}
    	return os;
    }
    
    int main(void) {
    	polynomials po1, po2, po3;
    	cin >> po1 >> po2;
    	po3 = po1 * po2;
    	cout << po3 << endl;
    }
    

    Result

  • 相关阅读:
    交换函数作业
    对本课程的期望及对老师的建议
    前一半元素与后一半元素交换
    使用多线程(newSingleThreadScheduledExecutor)创建一个定时任务
    js判断是否为整数
    kafka入门及使用(不用集成spring)
    Eclipse 必须安装的几个插件
    mybatis 批量插入/批量修改的写法
    maven pom.xml中出现错误failOnMissingWebXml Dynamic Web Module 3.1 requires Java 1.7 or newer,每次update后都会回滚到1.5解决方案
    Eclipse 插件安装及相关问题解决方案(svn、spring、gradle、git)
  • 原文地址:https://www.cnblogs.com/by-sknight/p/11450802.html
Copyright © 2020-2023  润新知