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; }
相关文章推荐
- [置顶] Java并发编程与技术内幕:ConcurrentHashMap源码解析
- hdu 5087(LIS变形)
- .NET Framework 各版本区别
- PayPal高级工程总监:读完这100篇论文 就能成大数据高手
- java多线程1
- Git 大杂烩(初学git 常用的命令)
- Sublime Text 3 快捷键汇总
- NodeJs――入门必看攻略
- ANSI C (1) —— 基础
- 分析飞机敌机的技巧-2
- 公司所用的技术和工具汇总
- Canonical 演示为任意发行版创建一个 Snap 商店是如何的简单
- Missing Number
- [326] Power of Three
- 《AngularJS》----$apply 与 $watch
- Ubuntu使用(一)——常用命令与软件安装配置
- 安卓技术介绍-android权限
- Eclipse工具使用技巧总结
- java集合复习2
- Android第三方框架(不断更新中)