原题:https://pintia.cn/problem-sets/994805342720868352/problems/994805509540921344
我记得以前上mooc时用的是链表解决,这里参考了柳婼(chuo)大神的方法,用两个double数组:arr[]存储第一组,ans[]保存结果,读取第二组数据时边读取边计算。
数组解决(推荐)
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include<stack>
#include<queue>
#include<sstream>
using namespace std;
/*
A1009 Product of Polynomials (25 分)
简单模拟,开两个double数组,arr[]存储第一组,ans[]保存结果,读取第二组数据时边读取边计算。
*/
const int MaxN = 2001;
int main() {
//freopen("input.txt", "r", stdin);
int k1,k2;
cin >> k1;
double arr[MaxN] = { 0.0 }, ans[MaxN] = { 0.0 };
for (int i = 0; i < k1; i++) {
int e; //指数(0-1000)
double a; //系数
cin >> e >> a;
arr[e] = a;
}
cin >> k2;
for (int i = 0; i < k2; i++) {
int e;
double a;
cin >> e >> a;
//a*x^e 与arr[]相乘
for (int j = 0; j <= 1000; j++) {
ans[e + j] += arr[j] * a;
}
}
int cnt = 0;
for (int i = 2000; i >= 0; i--) {
if (ans[i] != 0.0)
cnt++;
}
//cout << cnt;
printf("%d", cnt);
for (int i = 2000; i >= 0; i--) {
if (ans[i] != 0.0) {
//cout <<" "<< i << " " << ans[i];
printf(" %d %.1f", i, ans[i]);
}
}
printf("\n");
return 0;
}
链表解决
#include <stdio.h>
#include <stdlib.h>
/*Use LinkList*/
typedef struct PolyNode *Polynomial;
struct PolyNode {
int coef;
int expon;
Polynomial Link;//A pointer to the next node
};
typedef Polynomial List;
/*Read the coef from screen*/
//Attach a Node
void Attach(int c, int e, List *pRear) {
List P;
P = (List)malloc(sizeof(struct PolyNode));
P->coef = c;
P->expon = e;
P->Link = NULL;
(*pRear)->Link = P;
*pRear = P;
}
//Compare a and b
int Compare(int a, int b) {
if (a > b)return 1;
else if (a < b) return -1;
else return 0;
}
//First, Rear points to an empty node
List Polyread() {
int N,c,e;//N为非零系数项数
//L为返回链表,Rear最开始指向一个空节点,最后要删除,无表头节点
List Rear, L, t;
L = (List)malloc(sizeof(struct PolyNode));
L->Link = NULL;
Rear = L;
scanf("%d", &N);
while (N) {
scanf("%d %d", &c, &e);
Attach(c, e, &Rear);
N--;
}
//删除表头
t = L;
L = L->Link;
free(t);
return L;
};
/*Add Polynomial L1 and L2*/
List PolyAdd(List L1, List L2) {
List front, rear, temp;
int sum;
rear = (List)malloc(sizeof(struct PolyNode));
front = rear; //front 记录结果多项式链表头节点
//如果两个多项式都含有非零项
while (L1&&L2)
{
int e1 = L1->expon, e2 = L2->expon;
int c1 = L1->coef, c2 = L2->coef;
switch (Compare(e1,e2))
{
case 1:
Attach(c1, e1, &rear);
L1 = L1->Link;
break;
case -1:
Attach(c2, e2, &rear);
L2 = L2->Link;
break;
case 0:
sum = c1 + c2;
if (sum)
Attach(sum, e1, &rear);
L2 = L2->Link;
L1 = L1->Link;
}
}
//将未处理完的另一个多项式的所有节点复制
while (L1) {
Attach(L1->coef, L1->expon, &rear);
L1 = L1->Link;
}
while (L2) {
Attach(L2->coef, L2->expon, &rear);
L2 = L2->Link;
}
rear->Link = NULL;
//删除头结点,返回
temp = front;
front = front->Link;
free(temp);
return front;
}
/*Multiply two Polynomials*/
List PolyMult(List L1, List L2) {
List L, L_t, rear;
List t1 = L1, t2 = L2;
//其中有一个为空
if (!L1 || !L2)
return NULL;
L = (List)malloc(sizeof(struct PolyNode));
L_t= (List)malloc(sizeof(struct PolyNode));
//先用t1的第一项乘以t2,得L
rear = L;
while (t2) {
int e1 = t1->expon, e2 = t2->expon;
int c1 = t1->coef, c2 = t2->coef;
Attach(c1*c2, e1+e2, &rear);
t2=t2->Link;
}
//再依次相乘,再相加, 首先去掉L头结点,得到L
List Temp = L;
L = L->Link;
free(Temp);
t1 = t1->Link;//从t1的第二项开始加
while (t1) {
t2 = L2; rear = L_t;
while (t2) {
int e1 = t1->expon, e2 = t2->expon;
int c1 = t1->coef, c2 = t2->coef;
Attach(c1*c2, e1 + e2, &rear);
t2 = t2->Link;
}
L = PolyAdd(L, L_t->Link);
t1 = t1->Link;
}
//释放L_t
free(L_t);
return L;
}
void Polyprint(List L) {
if (L == NULL) {
printf("0 0\n");
return;
}
while (L->Link) {
printf("%d %d ", L->coef, L->expon);
L = L->Link;
}
printf("%d %d\n", L->coef, L->expon);
}
int main() {
List L1, L2, L;
L1 = Polyread();
L2 = Polyread();
L = PolyMult(L1,L2);
Polyprint(L);
L = PolyAdd(L1, L2);
Polyprint(L);
//getchar();
return 0;
}