PAT Advanced 1009 Product of Polynomials (25分)

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

Input Specification:

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?N?1???? N?2?? a?N?2???? ... N?K?? a?N?K????

where K is the number of nonzero terms in the polynomial, N?i?? and a?N?i???? (,) are the exponents and coefficients, respectively. It is given that 1, 0.

Output Specification:

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 Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 3 3.6 2 6.0 1 1.6

多项式相乘,我们需要注意去使用map进行存储,去除结果为0的,然后输出即可

#include <iostream>
#include <vector>
#include <map>
using namespace std;
struct poly{
    double coeff;
    int expo;
};
int main()
{
    int M;poly tmp;
    vector<poly> poly1,poly2;
    /**input*/
    cin>>M;
    while(M--){
        cin>>tmp.expo>>tmp.coeff;
        poly1.push_back(tmp);
    }
    cin>>M;
    while(M--){
        cin>>tmp.expo>>tmp.coeff;
        poly2.push_back(tmp);
    }
    map<int,double,greater<int>> m;
    /**计算*/
    for(int i=0;i<poly1.size();i++)
        for(int j=0;j<poly2.size();j++)
            m[poly1[i].expo+poly2[j].expo]+=poly1[i].coeff*poly2[j].coeff;
    /**去0*/
    for(auto it=m.begin();it!=m.end();it++)
        if(it->second==0) m.erase(it);
    /**输出*/
    cout<<m.size();
    for(auto it=m.begin();it!=m.end();it++)
        printf(" %d %.1f",it->first,it->second);
    system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/littlepage/p/12215791.html

时间: 2025-01-04 16:44:12

PAT Advanced 1009 Product of Polynomials (25分)的相关文章

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

目录 Problem Description Input Output Sample Sample Input Sample Output Solution Analysis Code Result Problem portal:1009 Product of Polynomials Description Input Output Sample Sample Input Sample Output Solution Analysis Code #include <bits/stdc++.h>

1009 Product of Polynomials (25分) 多项式乘法

1009 Product of Polynomials (25分) This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomi

1009 Product of Polynomials (25 分)

This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: 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?N?1???? N?2?? a?N?2?

PAT:1009. Product of Polynomials (25) AC

#include<stdio.h> #include<stdlib.h> #include<string.h> //[warning]double 输入%lf,输出%f struct arr { int exp; //指数 double cof; //系数 }arr[1005]; double ans[2010]; //下标是指数,内容是系数 int main() { memset(arr,0,sizeof(arr)); memset(ans,0,sizeof(ans)

1009. Product of Polynomials (25)——PAT (Advanced Level) Practise

题目信息: 1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This time, you are supposed to find A*B where A and B are two polynomials. Input Specification: Each input file contains one test case. Each c

1009 Product of Polynomials (25)(25 分)

1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are two polynomials. Input Specification: Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a pol

PAT 1009. Product of Polynomials (25)

1009. Product of Polynomials (25) This time, you are supposed to find A*B where A and B are two polynomials. Input Specification: Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomi

PAT (Advanced Level) 1009. Product of Polynomials (25)

简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> using namespace std; double a[3000],b[3000],c[3000]; int k; int main() { for(int i=0;i<=2000;i++) a[i]=b[i]=c[i]=0; int Max=-1

PAT甲题题解-1009. Product of Polynomials (25)-多项式相乘

多项式相乘 注意相乘结果的多项式要开两倍的大小!!! #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string.h> using namespace std; //两个多项式相乘 const int maxn=1000+5; int k; //原本定义个exp导致编译错误,没想到定义成exp1.exp2也会编译错误,