1002 A+B for Polynomials【PAT (Advanced Level) Practice】
Original question link:Preview question details - 1002 A+B for Polynomials (pintia.cn)
1.Original text of the title
This time, you are supposed to find A + B A+B A+B where A A A and B B B are two polynomials.
Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:
K K K N 1 N_1 N1 a N 1 a_{N_1} aN1 N 2 N_2N2 a N 2 a_{N_2} aN2 … N K N_K NK a N K a_{N_K} aNK
where K K K is the number of nonzero terms in the polynomial, N i N_i Ni and a N i a_{N_i} aNi ( i = 1 , 2 , ⋯ , K i=1, 2, \cdots , K i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1 ≤ K ≤ 10 1 \le K \le 10 1≤K≤10, 0 ≤ N K < ⋯ < N 2 < N 1 ≤ 1000 0 \le N_K < \cdots < N_2 < N_1 \le 1000 0≤NK<⋯<N2<N1≤1000.
Output Specification:
For each test case you should output the sum of A A A and B B B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.
Sample Input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output:
3 2 1.5 1 2.9 0 3.2
2. Title translation
这次,你应该找到 A + B A+B A+B, inside A A Asum B B B are two polynomials.
Input specifications:
Each input file contains a test case. Each case occupies 2 lines, each line contains polynomial information:
K KK N 1 N_1 N1 a N 1 a_{N_1}aN1 N 2 N_2N2 a N 2 a_{N_2}aN2 … N K N_K NK a N K a_{N_K}aNK
Naka K K K is the number of non-zero terms in the polynomial, N i N_i Nisum a N i a_{N_i} aNi( i = 1 , 2 , ⋯ , K i=1, 2, \cdots , K i=1,2,⋯,K) Separation is the exponential sum system. Bichi 1 ≤ K ≤ 10 1 \le K \le 10 1≤K≤10, 0 ≤ N K < ⋯ < N 2 < N 1 ≤ 1000 0 \le N_K < \cdots < N_2 < N_1 \le 1000 0≤NK<⋯<N2<N1≤1000。
Output specification:
For each test case you should output in one line A A Asum B B The sum of B has the same format as the input. Please note that there cannot be extra spaces at the end of each line. Please be accurate to 1 decimal place.
Example input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Example output:
3 2 1.5 1 2.9 0 3.2
3. Problem-solving ideas
3.1 Question analysis
To calculate the sum of two polynomials, enter the number of terms of the two polynomials in two lines, and enter the exponent and coefficient of each term in exponential decreasing order. The sum of the polynomials is output in the same format.
3.2 Basic ideas
Algorithm to implement polynomial addition. Polynomials are usually implemented in two ways: array implementation and linked list implementation.
Linked list implementation: Each item of the polynomial is a node of the linked list, and the node records the coefficient and exponent.
Array implementation: Each element of the array represents the coefficient of the polynomial, and the array subscript represents the exponent of the polynomial. Because the index N ≤ 1000 N \leq 1000 N≤1000 , so the number of pairs is large and small 1001
.
3.3 Detailed steps
Linked list implementation
- Define the node structure and polynomial type:
- Use structure
Node
to represent each term of the polynomial, including coefficientsCoefficient
, exponentExponent
and pointer to the next Pointer to itemNext
. - Definition
Polynomial
is a pointer to theNode
structure, representing the entire polynomial. - Use the
Attach
function to append new nodes to the tail of the polynomial.
- Use structure
- Comparison function:
- defines the
COMPARE
function, which is used to compare the size of two indices and returns -1, 0 or 1 to indicate less than, equal to or greater than respectively.
- defines the
- Read the polynomial:
- Use the
ReadIn
function to read a polynomial from the input. First read the number of non-zero terms of the polynomialk
, and then read the exponentn
and coefficienta
of each term through a loop, Call theAttach
function to append each term to the tail of the polynomial. Returns the head pointer of the polynomial.
- Use the
- Print polynomials:
- Use the
PrintOut
function to print polynomials. First count the number of non-zero terms of the polynomial, and then output the number of non-zero terms and the exponent and coefficient of each term.
- Use the
- Add polynomials:
- Use the
Add
function to add polynomials. Create a head node of the result polynomial, and then use a loop starting from the first node after the head node to compare the exponents of the corresponding nodes of the two polynomials in sequence:- If the exponents are equal, the coefficients are added, and if the result is not zero, the
Attach
function is called and appended to the tail of the resulting polynomial. - If the exponents are not equal, the node corresponding to the larger exponent is appended to the tail of the resulting polynomial.
- If the nodes of a polynomial are traversed, the remaining nodes of the other polynomial are appended to the tail of the resulting polynomial.
- If the exponents are equal, the coefficients are added, and if the result is not zero, the
- Returns the head pointer of the result polynomial.
- Use the
- Main function:
- Read two polynomials and call the
Add
function to add them. - Print the added polynomial.
- Read two polynomials and call the
Array implementation
- Read the first polynomial:
- Read the number of non-zero terms of the first polynomial via.
scanf
K1
- Create an array
poly1
to store the coefficients of the first polynomial. The subscript of the array represents the exponent of the polynomial, which is initialized to0
. - Through looping, read the exponent
N
and coefficient of each itema
, and store the coefficient in thepoly1
array corresponding position in .
- Read the number of non-zero terms of the first polynomial via.
- Read the second polynomial:
- Read the number of non-zero terms of the second polynomial through.
scanf
K2
- Create an array
poly2
to store the coefficients of the second polynomial. The subscript of the array represents the exponent of the polynomial and is initialized to 0. - Through looping, read the exponent
N
and coefficient of each itema
, and store the coefficient in thepoly2
array corresponding position in .
- Read the number of non-zero terms of the second polynomial through.
- Calculation results:
- Create an array
poly
to store the coefficients of the added polynomial. The subscript of the array represents the exponent of the polynomial and is initialized to 0. - Use a loop to traverse the coefficient arrays of the two polynomials, calculate the sum of the coefficients of the corresponding exponents in order from high order to low order, and store the result in the
poly
array.
- Create an array
- Output result:
- Use a variable
cnt
to record the number of non-zero terms in the added polynomial. - Use a loop to traverse the
poly
array and output the number of non-zero terms of the added polynomial as well as the exponent and coefficient of each term. The output format requires 1 digit after the decimal point.
- Use a variable
4. Reference answer
Linked list implementation
#include <stdio.h>
#include <malloc.h>
// 定义节点结构体
typedef struct Node *PtrToNode;
typedef struct Node {
double Coefficient; // 系数
int Exponent; // 指数
PtrToNode Next; // 指向下一个节点的指针
} PNode;
// 定义多项式类型
typedef PtrToNode Polynomial;
// 比较函数,用于比较两个指数的大小
int COMPARE(int a, int b) {
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
// 将节点(包含系数和指数)附加到多项式的尾部
void Attach(double coefficient, int exponent, PtrToNode *ptr) {
PtrToNode temp;
// 分配新节点
temp = malloc(sizeof(PNode));
temp->Coefficient = coefficient;
temp->Exponent = exponent;
temp->Next = NULL;
(*ptr)->Next = temp; // 将新节点连接到尾部
*ptr = temp; // 更新尾部指针为新节点
}
// 从输入中读取多项式
Polynomial ReadIn() {
PtrToNode front, tail;
double a;
int k, n;
scanf("%d", &k);
if (!k)
return NULL; // 如果多项式的非零项数为0,则返回空指针
tail = malloc(sizeof(PNode));
front = tail;
front->Exponent = -1;
while (k--) {
scanf("%d %lf", &n, &a);
Attach(a, n, &tail);
}
return front; // 返回多项式的头指针
}
// 打印多项式
void PrintOut(Polynomial P) {
Polynomial s;
int k = 0;
s = P->Next;
while (s) {
k++;
s = s->Next;
}
printf("%d", k);
if (k) {
for (s = P->Next; s; s = s->Next)
printf(" %d %.1f", s->Exponent, s->Coefficient);
}
printf("\n");
}
// 多项式相加
Polynomial Add(Polynomial a, Polynomial b) {
PtrToNode front, tail;
double sum;
// 创建结果多项式的头节点
tail = malloc(sizeof(PNode));
front = tail;
front->Exponent = -1;
// 跳过头节点,从第一个节点开始相加
a = a->Next;
b = b->Next;
while (a && b)
switch (COMPARE(a->Exponent, b->Exponent)) {
case -1:
Attach(b->Coefficient, b->Exponent, &tail);
b = b->Next;
break;
case 0:
sum = a->Coefficient + b->Coefficient;
if (sum)
Attach(sum, a->Exponent, &tail);
a = a->Next;
b = b->Next;
break;
case 1:
Attach(a->Coefficient, a->Exponent, &tail);
a = a->Next;
break;
}
// 将剩余的节点附加到结果多项式的尾部
for (; a; a = a->Next)
Attach(a->Coefficient, a->Exponent, &tail);
for (; b; b = b->Next)
Attach(b->Coefficient, b->Exponent, &tail);
tail->Next = NULL; // 结果多项式的尾部指针置空
return front; // 返回结果多项式的头指针
}
// 主函数
int main() {
Polynomial A, B, S;
// 读取两个多项式
A = ReadIn();
B = ReadIn();
// 将两个多项式相加
S = Add(A, B);
// 打印结果多项式
PrintOut(S);
return 0;
}
Array implementation
#include <stdio.h>
#define MAXN 1001
int main() {
int K1, K2, N, i;
double a;
// 读取第一个多项式
scanf("%d", &K1);
double poly1[MAXN] = {
0}; // 多项式1的系数数组,下标表示指数
for (i = 0; i < K1; ++i) {
scanf("%d %lf", &N, &a);
poly1[N] = a;
}
// 读取第二个多项式
scanf("%d", &K2);
double poly2[MAXN] = {
0}; // 多项式2的系数数组,下标表示指数
for (i = 0; i < K2; ++i) {
scanf("%d %lf", &N, &a);
poly2[N] = a;
}
//计算结果
double poly[MAXN] = {
0};
int cnt= 0;
for (i = 1000; i >= 0; --i) {
if (poly1[i] + poly2[i] != 0.0){
cnt++;
poly[i] = poly1[i] + poly2[i];
}
}
// 输出
printf("%d",cnt);
for (i = 1000; i >= 0; --i) {
if (poly[i] != 0.0)
printf(" %d %.1lf", i, poly1[i] + poly2[i]);
}
return 0;
}