1009. Product of Polynomials (25)

1009. Product of Polynomials (25)

目录

原题

1009. Product of Polynomials (25)链接

思路

1. 读入两个多项式,分别存储于链表A和B。
2. 从链表A头开始,节点和B中的每一个节点相乘,即系数相乘,次数相加,然后在C链表里寻找次数大于等于结果的一项。如果有相等,则两个节点相加,即系数相加,次数不变。如果只找到大于的一项,则向后插入该节点。
3.  输出链表C,系数为零的节点不输出。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cfloat>
#include <cstring>

struct node {
    double co;
    int ex;
    node *next;
};

struct list {
    int count;
    node *head;
    node *rear;
};

node *find(list *ptr, node *key);
node *multiply(node *first, node *second);
void insert(list *ptr, node *key);
void print(list *ptr);

int main(void) {
    setvbuf(stdin, new char[1 << 20], _IOFBF, 1 << 20);
    setvbuf(stdout, new char[1 << 20], _IOFBF, 1 << 20);

    int k;
    list *list1 = new list;
    list *list2 = new list;
    scanf("%d", &k);
    node *temp = new node;
    temp->next = nullptr;
    scanf("%d %lf", &(temp->ex), &(temp->co));
    list1->count = k;
    list1->head = list1->rear = temp;
    for (int i = 1; i < k; i++) {
        node *temp = new node;
        scanf("%d %lf", &(temp->ex), &(temp->co));
        temp->next = nullptr;
        list1->rear->next = temp;
        list1->rear = temp;
    }

    scanf("%d", &k);
    temp = new node;
    temp->next = nullptr;
    scanf("%d %lf", &(temp->ex), &(temp->co));
    list2->count = k;
    list2->head = list2->rear = temp;
    for (int i = 1; i < k; i++) {
        node *temp = new node;
        temp->next = nullptr;
        scanf("%d %lf", &(temp->ex), &(temp->co));
        list2->rear->next = temp;
        list2->rear = temp;
    }

    node *ptr1 = list1->head;
    list *list3 = new list;
    memset(list3, 0, sizeof(list));
    for (int i = 0; i < list1->count; i++) {
        node *ptr2 = list2->head;
        for (int j = 0; j < list2->count; j++) {
            node *ptr3 = multiply(ptr1, ptr2);
            insert(list3, ptr3);
            ptr2 = ptr2->next;
        }
        ptr1 = ptr1->next;
    }
    int count = 0;
    node *ptr3 = list3->head;
    for (int i = 0; i < list3->count; i++) {
        if (fabs(ptr3->co) > FLT_EPSILON) {
            count++;
        }
        ptr3 = ptr3->next;
    }
    list3->count = count;
    print(list3);
    node *now = list1->head;
    node *next;
    for (int i = 0; i < list1->count; i++) {
        next = now->next;
        delete now;
        now = next;
    }

    now = list2->head;
    for (int i = 0; i < list2->count; i++) {
        next = now->next;
        delete now;
        now = next;;
    }


    now = list3->head;
    for (int i = 0; i < list3->count; i++) {
        next = now->next;
        delete now;
        now = next;
    }

    delete list1;
    delete list2;
    delete list3;

    return 0;
}

node *find(list *ptr, node *key) {
    node *now = ptr->head;

    if (!now || now->ex < key->ex) {
        return nullptr;
    }

    while (now->next && now->next->ex > key->ex) {
        now = now->next;
    }
    if (now->next && now->next->ex == key->ex) {
        return now->next;
    }
    else {
        return now;
    }
}

node *multiply(node *first, node *second) {
    node *third = new node;
    memset(third, 0, sizeof(node));
    third->co = first->co * second->co;
    third->ex = first->ex + second->ex;

    return third;
}
void insert(list *ptr, node *key) {
    node *third = find(ptr, key);
    if (!third) {
        key->next = ptr->head;
        ptr->head = key;
        ptr->count++;
    }
    else if (third->ex == key->ex) {
        third->co += key->co;
        delete key;
    }
    else {
        key->next = third->next;
        third->next = key;
        ptr->count++;
    }
    return;
}
void print(list *ptr) {
    printf("%d", ptr->count);
    node *third = ptr->head;
    while(third) {
        if (fabs(third->co) > FLT_EPSILON) {
            printf(" %d %.1lf", third->ex, third->co);
        }
        third = third->next;
    }
    putchar('\n');
    return;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值