PTA 甲 1009

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了

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值