PAT甲级1002 链表实现方法

题目:

1002. A+B for Polynomials (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
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



题目大意就是给定两个多项式 F1=aN1*x^N1+aN2*x^N2+aN3*x^N3+.....和F2=bN1*x^N1+bN2*x^N2+bN3*x^N3+..... 求两个多项式的和
#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;
    }
};
int main()
{
    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
*/

 这道题交了无数回,踩过了所有的坑。。。首先多项式的系数可以为负数,当系数减到0的时候要删掉这个节点。还有就是精确到小数点后一位。最后要控制格式,答案末尾直接输出回车,不要有空格。最后一组样例的结果应该是0,同样不能输出空格,没过的可能需要注意一下这点。

猜你喜欢

转载自www.cnblogs.com/LowBee/p/8976040.html