PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分)

时间:2022-07-26
本文章向大家介绍PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1002 A+B for 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​​​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N​K​​<⋯<N​2​​<N​1​​≤1000.

Output Specification:

For each test case you should output the sum 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 to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 2 1.5 1 2.9 0 3.2

polynomial释义:多项式

题意为实现一元多项式的相加

那就是数据结构链表入门题了~,采用维克托(vector)存coef和val即系数与指数两个变量

置两个指针分别初始为两个 指向两个多项式起始节点由于两个多项式的指数都是按照递减序输入的

分三种情况

1.第一个指针所指的多项式某项指数大于第二个指针所指的多项式某项指数,那么while循环不断右移第一个多项式的指针直到大于等于第一个多项式指针所指指数且将所指结点压入vector中

2.第一个指针所指的多项式某项指数等于第二个指针所指的多项式某项指数,那么如果两个所指的多项式某项指数相加为零两个指针都右移,否则将结点(两个指针所指多项式某项指数,两个所指的多项式某项指数之和)

3.第一个指针所指的多项式某项指数小于第二个指针所指的多项式某项指数,那么while循环不断右移第二个多项式的指针直到大于等于第一个多项式指针所指指数且将所指结点压入vector中

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 505
#define lb(x) (x&(-x))
const double eps = 1e-6;
using namespace std;
inline ll read()
{
 char ch = getchar(); ll s = 0, w = 1;
 while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
 while (ch >= 48 && ch <= 57) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
 return s * w;
}
inline void write(ll x)
{
 if (x < 0)putchar('-'), x = -x;
 if (x > 9)write(x / 10);
 putchar(x % 10 + 48);
}
struct node
{
    int coef;
    double val;
    node(int a=0,double b=0)
    {
        coef=a,val=b;
    }
}p1[1005],p2[1005];
vector<node>ans;
int main()
{
    rg a1=read();
    for(rg i=1;i<=a1;i++)
    {
        cin>>p1[i].coef>>p1[i].val;
    }
    rg a2=read();
    for(rg i=1;i<=a2;i++)
    {
        cin>>p2[i].coef>>p2[i].val;
    }
    rg i=1,j=1;
    //cout<<a1<<" "<<a2<<endl;
    while(i<=a1&&j<=a2)
    {
        //cout<<p1[i].coef<<" "<<p2[j].coef<<endl;
        if(p1[i].coef>p2[j].coef)
        {
            while(p1[i].coef>p2[j].coef&&i<=a1&&p1[i].val)
            {

                ans.push_back(p1[i]);
                i++;
                if(i>a1)break;
            }
        }
        else if(p1[i].coef==p2[j].coef)
        {
            if(!(p1[i].val+p2[j].val))
            {
                i++,j++;
                continue;
            }
            ans.push_back(node{p1[i].coef,p1[i].val+p2[j].val});
            i++,j++;
        }
        else if(p1[i].coef<p2[j].coef)
        {
            while(p1[i].coef<p2[j].coef&&j<=a2&&p2[j].val)
            {
                ans.push_back(p2[j]);
                //cout<<ans.size()<<endl;
                j++;
                if(j>a2)break;
            }
        }
    }
    while(i<=a1&&p1[i].val)
    {
        ans.push_back(p1[i]);
        i++;
    }
    while(j<=a2&&p2[j].val)
    {
        ans.push_back(p2[j]);
        j++;
    }
    if(!ans.size())
    {
        return 0*putchar('0');
    }
    cout<<ans.size()<<" ";
    for(rg i=0;i<ans.size();i++)
    {
        if(i==ans.size()-1)
        {
            cout<<ans[i].coef<<" "<<setiosflags(ios::fixed)<<setprecision(1)<<ans[i].val<<endl;
        }
        else cout<<ans[i].coef<<" "<<setiosflags(ios::fixed)<<setprecision(1)<<ans[i].val<<" ";
    }
    return 0;
}