您的位置:首页 > 其它

1009. Product of Polynomials (25)

2016-06-27 08:54 281 查看

1009. Product of Polynomials (25)

目录

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: