PAT甲级 1002 A+B for Polynomials

#include <iostream>
#include <cstdio>
using namespace std;

typedef struct nodee
{
    int n = 0;
    double a = 0;
    nodee *next = NULL;
} node;
int main()
{
    int m, n;
    cin >> m;
    node *pm = new node;
    node *pn = new node;

    for (int i = 0; i < m; i++)
    {
        int x;
        double y;
        cin >> x >> y;

        node *p = new node;

        p->a = y;
        p->n = x;
        p->next = pm->next;
        pm->next = p;

        p = NULL;
        delete p;
    }
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int x;
        double y;
        cin >> x >> y;
        node *p = new node;
        p->a = y;
        p->n = x;
        p->next = pn->next;
        pn->next = p;
        p = NULL;
        delete p;
    }
    node *t = new node;
    int count = 0;
    node *p = pm->next, *q = pn->next;
    while (q != NULL && p != NULL)
    {
        if (p->n == q->n)
        {
            int flag = p->a + q->a;
            if (flag == 0)
            {
                p = p->next;
                q = q->next;
            }
            else
            {
                node *temp = new node;
                temp->n = p->n;
                temp->a = p->a + q->a;
                temp->next = t->next;
                t->next = temp;
                temp = NULL;
                delete temp;
                count++;
                p = p->next;
                q = q->next;
            }
        }
        else if (p->n < q->n)
        {
            node *temp = new node;
            temp->n = p->n;
            temp->a = p->a;
            temp->next = t->next;
            t->next = temp;
            temp = NULL;
            delete temp;
            count++;
            p = p->next;
        }
        else if (p->n > q->n)
        {
            node *temp = new node;
            temp->n = q->n;
            temp->a = q->a;
            temp->next = t->next;
            t->next = temp;
            temp = NULL;
            delete temp;
            count++;
            q = q->next;
        }
    }
    while (p != NULL)
    {
        node *temp = new node;
        temp->n = p->n;
        temp->a = p->a;
        temp->next = t->next;
        t->next = temp;
        temp = NULL;
        delete temp;
        count++;
        p = p->next;
    }
    while (q != NULL)
    {
        node *temp = new node;
        temp->n = q->n;
        temp->a = q->a;
        temp->next = t->next;
        t->next = temp;
        temp = NULL;
        delete temp;
        count++;
        q = q->next;
    }
    
    cout << count;
    for (node *pstr = t->next; pstr != NULL; pstr = pstr->next)
    {
        cout << " ";
        printf("%d %.1lf", pstr->n, pstr->a);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值