PAT Grade A 1002 Linked List Implementation Method

topic:

1002. A+B for Polynomials (25)

time limit
400 ms
memory limit
65536 kB
code length limit
16000 B
Judgment procedure
Standard
author
CHEN, Yue

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 N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.

 

Output

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 



The gist of the title is to give two polynomials F1=aN1*x^N1+aN2*x^N2+aN3*x^N3+.....and F2=bN1*x^N1+bN2* x^N2+bN3*x^N3+..... Find the sum of two polynomials
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<iomanip>
using namespace std;
template <class T>
class Link
{
public:
    T data1,data2;
    Link<T> *next;
    Link(const T d1,const T d2,Link<T> *nex=NULL)
    {
        data1=d1;
        data2=d2;
        next=nex;
    }
    Link()
    {
        data1=0;
        data2=0;
        next=NULL;
    }
};
template <class T>
class InkLink
{
private:
    Link<T> *head;
    int lon;
public:
    InkLink()
    {
        head=new Link<T>;
        lon=0;
    }
    void display2()
    {
        Link<T> *p;
        p=head->next;
        cout << lon ;
        while(p!=NULL)
        {
            printf(" ");
            printf("%d %.1lf",(int)p->data1,p->data2);
            p=p->next;
        }
        cout <<endl;
    }
    bool insertDesc(const T value1,const T value2)
    {
        if(head->next==NULL)
        {
            head->next=new Link<T>(value1,value2);
            lon++;
            return true;
        }
        Link<T> *p,*q;
        p=head->next;
        q=head;
        while(p!=NULL&&((p->data1)>value1))
        {
            q=p
            p=p->next;
        }
        if(p==NULL||((p->data1)<value1))
        {

            q->next=new Link<T>(value1,value2,p);
            lon++;
        }
        else if(p->data1==value1)
        {
            p->data2+=value2;
            if(p->data2==0)
            {
                if(p->next!=NULL)q->next=p->next;
                else q->next=NULL;
                delete p;
                lon--;
            }
        }
        return true;
    }
    void clearLink()
    {
        Link<T> *p,*q;
        p=head->next;
        q=p
        while(p!=NULL)
        {
            p=p->next;
            delete q;
            q=p
        }
        lon=0;
        head->next=NULL;
    }
};
intmain()
{
    InkLink<double> l;
    int n;
    double a,b;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf",&a,&b);
            l.insertDesc(a,b);
        }
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf",&a,&b);
            l.insertDesc(a,b);
        }
        l.display2();
        l.clearLink();
    }
    return 0;
}
/*
2 1 2.4 0 3.2
2 2 1.5 0 -3.2
*/

 This question has been handed in countless times, and I have stepped on all the pits. . . First of all, the coefficient of the polynomial can be negative. When the coefficient is reduced to 0, the node should be deleted. Also, it is accurate to one decimal place. Finally, you need to control the format, and output the carriage return directly at the end of the answer without spaces. The result of the last set of samples should be 0, and no spaces can be output. Those who haven't passed it may need to pay attention to this.

Guess you like

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