[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>
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

原文地址:https://www.cnblogs.com/by-sknight/p/11450802.html

时间: 2024-10-03 03:28:48

[PTA] PAT(A) 1009 Product of Polynomials (25 分)的相关文章

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?

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: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 分)

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)

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)——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

[PTA] PAT(A) 1007 Maximum Subsequence Sum (25 分)

目录 Problem Description Input Output Sample Sample Input Sample Output Solution Analysis Code Problem portal: 1007 Maximum Subsequence Sum (25 分) Description Given a sequence of $K$ integers { $N_{1}?$, $N_{2}?$, $...$, $N_{K}$ }. A continuous subsequ

[PTA] PAT(A) 1012 The Best Rank (25 分)

目录 Problem Solution Analysis Code Problem portal: 1012 The Best Rank (25 分) Solution Analysis ?一名学生有三门科目,以及计算出的一个平均成绩,每一个成绩都会有一个排名,现在想要让你输出某一个同学最高的排名(四种成绩排名的最高),以及对应的科目 ?如果给定的同学的四种课程排名的名次信息已经存在,那么就很简单,在里面找到最小的名次,以及对应的科目输出即可. ?可以创建四个数组,数组的每个元素存储某一门科目的