目录
- 1009 Product of Polynomials
- 题目原题
- Input Specification:
- Output Specification:
- Sample Input:
- Sample Output:
- 注意点:
- 一开始的代码
- 改进后如下
1009 Product of Polynomials
题目原题
This time, you are supposed to find A×B where A and 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 N1 aN1 N2 aN2 ... *N**K* aNK
where K is the number of nonzero terms in the polynomial, *N**i* and aNi (i=1,2,?,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10, 0≤*N**K<?<N2<N*1≤1000.
Output Specification:
For each test case you should output the product of A and 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 up to 1 decimal place.
Sample Input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output:
3 3 3.6 2 6.0 1 1.6
生词一览:
polynomials 多项式
exponents 指数
coefficients 系数
题目大意,就是两个没有零项式的多项式相乘,输出他们的结果,
注意点:
① 这道题目不需要用两个数组
② 在计算他们的乘积的时候,要先把他们的乘积的数组算出来.
这个非常的重要,Sumc一定要在C[e]都算好了在统计个数
因为C[e]在计算中,可能会出现一正一负的,可以消去的同类项.
会多计算两次C[e] 一般这就是测试点0通不过的原因
③ 最好在输出的时候,是不可以输出非零项的.
一开始的代码
代码如下
#include<cstdio>
#include<iostream>
using namespace std;
int main(void) {
int K1, K2, Maxa = 0, Maxb = 0, e, Sumc = 0; //Sumc就是计算乘起来之后一共有几个非零的多项式
double c;
double A[1005] = { 0 }, B[1005] = { 0 }, C[2005] = { 0 };
scanf("%d", &K1);
for (int i = 0; i < K1; i++) {
scanf("%d%lf", &e, &c);
if (e > Maxa) Maxa = e; //这是计算乘积的时候的循环最大值
A[e] = c;
}
scanf("%d", &K2);
for (int i = 0; i < K2; i++) {
scanf("%d%lf", &e, &c);
if (e > Maxb) Maxb = e; //这是计算乘积的时候的循环最大值
B[e] = c;
}
for (int i = 0; i <= Maxa; i++) {
if (A[i] != 0.0) {
for (int j = 0; j <= Maxb; j++) {
if (B[j] != 0.0) {
e = i + j;
c = A[i] * B[j];
if (C[e] == 0.0) {
C[e] = c;
}
else
C[e] = C[e] + c;
}
}
}
}
//这个非常的重要,Sumc一定要在C[e]都算好了在统计个数
//因为C[e]在计算中,可能会出现一正一负的,可以消去的同类项.
//会多计算两次C[e]
for (int i = 0; i <= Maxa + Maxb; i++) {
if (C[i] != 0) {
Sumc++;
}
}
//第一个测试点有0 项,要删除,不能输出.
printf("%d", Sumc);
for (int i = Maxa + Maxb; i >= 0; i--) {
if (C[i] != 0.0) {
printf(" %d %.1lf", i, C[i]);
}
}
return 0;
}
改进后如下
#include<cstdio>
#include<iostream>
using namespace std;
int main(void) {
int K1, K2,e, Sumc = 0; //Sumc就是计算乘起来之后一共有几个非零的多项式
double c;
double A[1005] = { 0 }, B[1005] = { 0 }, C[2005] = { 0 };
scanf("%d", &K1);
for (int i = 0; i < K1; i++) {
scanf("%d%lf", &e, &c);
A[e] = c;
}
scanf("%d", &K2);
for (int i = 0; i < K2; i++) {
scanf("%d%lf", &e, &c);
int Te = e;
double Tc = c; //每一次写好了,要重新赋值
for (int j = 0; j <= 1002; j++) {
e = j + e;
c = A[j] * c;
if (c != 0) { //不计算非零项
if (C[e] == 0.0) {
C[e] = c;
}
else
C[e] = C[e] + c;
}
e = Te; c = Tc; //重新赋值
}
}
//这个非常的重要,Sumc一定要在C[e]都算好了在统计个数
//因为C[e]在计算中,可能会出现一正一负的,可以消去的同类项.
//会多计算两次C[e]
for (int i = 0; i <= 2002; i++) {
if (C[i] != 0) {
Sumc++;
}
}
//第一个测试点有0 项,要删除,不能输出.
printf("%d", Sumc);
for (int i = 2002; i >= 0; i--) {
if (C[i] != 0.0) {
printf(" %d %.1lf", i, C[i]);
}
}
return 0;
}
原文地址:https://www.cnblogs.com/a-small-Trainee/p/12342118.html