02-Linear structure 2 Multiplication and addition of unary polynomials (20 points)

**Title source: Chinese University MOOC-Chen Yue, He Qinming-Data Structure-Spring 2018
Author : DS Course Group
Unit: Zhejiang University**

Problem description:
Design functions to find the product and sum of two unary polynomials, respectively.
Input format:
The input is divided into 2 lines, each line first gives the number of non-zero polynomial terms, and then input a polynomial non-zero term coefficient and exponent in exponential descending manner (the absolute value is an integer not exceeding 1000). The numbers are separated by spaces.
Output format:
The output is divided into 2 lines, and the coefficients and exponents of the product polynomial and the non-zero term of the sum polynomial are output in the exponential descending manner. Numbers are separated by spaces, but no extra spaces at the end. A zero polynomial should output 0 0.
Input sample:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
Output sample:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

Answer: When I wrote this question, the idea was very strange==, please do not imitate, use two maps to store the corresponding information of two polynomials {exponent: coefficient}, and use three sets, two of which store two sets respectively. The exponent information of the polynomial is used to take the coefficients from the map for operation, and another set is used for the result generation (because the set is automatically sorted). It is still recommended to use an array with an open size of 1001 for implementation, which is more direct. I will paste the code for the array implementation.

#include <iostream>
#include <map>
#include <set>
using namespace std;
map<int,int> ExpAndNumA,ExpAndNumB,result;
set<int> ExpA,ExpB,resultSet;
void input(map<int,int> &SomeoneMap,set<int> &SomeoneSet,int N)
{
    for(int i=0; i<N; i++)
    {
        int num,exp;
        cin>>num>>exp;
        SomeoneMap[exp]=num;
        SomeoneSet.insert(exp);
    }
}
void add()
{
    for(auto it=ExpA.begin(); it!=ExpA.end(); it++)
    {
        resultSet.insert(*it);
        result[*it]=ExpAndNumA[*it];
    }
    for(auto it=ExpB.begin(); it!=ExpB.end(); it++)
    {
        if(result.count(*it)==0)
        {
            resultSet.insert(*it);
            result[*it]=ExpAndNumB[*it];
        }
        else
            result[*it]+=ExpAndNumB[*it];
    }
}
void multi()
{
    for(auto it=ExpA.begin(); it!=ExpA.end(); it++)
    {
        for(auto it2=ExpB.begin(); it2!=ExpB.end(); it2++)
        {
            int newExp=*it+*it2;
            int newNum=ExpAndNumA[*it]*ExpAndNumB[*it2];
            if(result.count(newExp)==0)
            {
                resultSet.insert(newExp);
                result[newExp]=newNum;
            }
            else
                result[newExp]+=newNum;
        }
    }
}
void output()
{
    bool flag=0;
    for(auto rit=resultSet.rbegin(); rit!=resultSet.rend(); rit++)
    {
        if(result[*rit]!=0)
        {
            flag=1;
            if(rit==resultSet.rbegin())
            {
                cout<<result[*rit]<<" "<<*rit;
            }
            else
            {
                cout<<" "<<result[*rit]<<" "<<*rit;
            }
        }
    }
    if(!flag)
        cout<<"0 0";
}
int main()
{
    int N1,N2;
    cin>>N1;
    input(ExpAndNumA,ExpA,N1);
    cin>>N2;
    input(ExpAndNumB,ExpB,N2);
    result.clear();
    resultSet.clear();
    multi();
    output();
    cout<<endl;
    result.clear();
    resultSet.clear();
    add();
    output();
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324523639&siteId=291194637