1009 Product of Polynomials
作者 CHEN, Yue
单位 浙江大学
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 ... NK aNK
where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10, 0≤NK<⋯<N2<N1≤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
代码
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#define max_num 11
#define max_n 3000
int main() {
double inputA[max_num]; int inputA_num[max_num];//An & n
double inputB[max_num]; int inputB_num[max_num];//Bn & n
double output[max_n];//output
for (int i = 0; i < max_n; i++) {
output[i] = 0;
}
int Acount,Bcount,outn=0;
//inputA
cin >> Acount;
for (int i = 0; i < Acount; i++) {
cin >> inputA_num[i];
cin >> inputA[i];
}
//inputB
cin >> Bcount;
for (int i = 0; i < Bcount; i++) {
cin >> inputB_num[i];
cin >> inputB[i];
}
//merge
//1.mulip
for (int i = 0; i < Acount; i++) {
for (int j = 0; j < Bcount; j++) {
int sum = inputA_num[i] + inputB_num[j]; //指数加
output[sum] = output[sum] + inputA[i] * inputB[j]; //系数×再和原来的系数和相加
}
}
for (int i = 0; i <= 2000; ++i)
if (output[i] != 0)
++outn;
cout << outn;
for (int i = 2000; i >=0; --i)
if (output[i] != 0)
cout << " " << i << " " << fixed << setprecision(1) << output[i];
return 0;
}
测试0提示:
一开始最后计算outn时判断用的是>0。一直不对
之后在网上搜了几个ac的代码,发现他们用的都是!=0
怀疑虽然题干中1<NK<1000,但用例里Nk可能就存在负数
改为!=0后就ac了