A1009. Product of Polynomials (25)

本文介绍了一种计算两个多项式乘积的方法,并提供了两种不同的实现方案。通过使用数组和结构体来存储多项式的幂次和系数,实现了高效的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 

题目大意:

计算两个多项式的乘积。

思路:

(1)用数组下标存放幂次,下标对应的元素存放系数。double型数组。 最高幂次可达到2000,数组应大于2000。

因有些幂次最后可能为0,非零项应最后统计。本题不需考虑多项式相乘的顺序;

(2)可加入结构体,存入第一组数据,在读入第二组数据的同时直接处理。

(1)代码如下:

#include <cstdio>

const int max_n = 2010;
int main()
{
	int k, exp, count = 0;
	double coe;	
	double a[max_n] = {}, b[max_n] = {}, c[max_n] = {};	 
	
	scanf ("%d", &k);
	while (k--)
	{
		scanf ("%d %lf", &exp, &coe);
		a[exp] = coe;							//数组下标为幂次,其中元素为该幂次的系数; 
	}
	
	scanf ("%d", &k);
	while (k--)
	{
		scanf ("%d %lf", &exp, &coe);
		b[exp] = coe;	
	}
	
	for (int i = max_n - 1; i >= 0 ; i--)			//a[max_n]处存放空字符'\0',非0,不应加入考虑; 否则会影响对非零项的统计;
	{
		if (a[i] != 0)						//系数为零项,相乘无意义,下同。 
		{
			for (int j = max_n - 1; j >= 0; j--)
			{
				if (b[j] != 0)
					c[i + j] += a[i] * b[j];	//相乘后的幂次即为两幂次的和; 
			}
		}
	}

	for (int i = 0; i < max_n; i++)		
	{
		if (c[i] != 0)		count++;  			//累计非零项系数的项数; 
	}

	printf ("%d", count);
	for (int i = max_n - 1; i >= 0; i--)
	{
		if (c[i] != 0)
		{
			printf (" %d %.1f", i, c[i]);         
		}
	}
	
	return 0;
} 

(2)代码如下:

#include <cstdio>

struct poly
{
	int exp;
	double cof;
} poly[1001]; // 存入第一个多项式 

double ans[2010] = {};
int main()
{
	int n, m, number = 0;
	scanf ("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf ("%d %lf", &poly[i].exp, &poly[i].cof);
	}
	
	scanf ("%d", &m);                    //两次的多项式项数要分开记录;用于记录存入数据的结构体的项数;
	for (int i = 0; i < m; i++)
	{
		int exp;
		double cof;
		scanf ("%d %lf", &exp, &cof);
		for (int j = 0; j < n; j++)            //存入数据与第一组多项式所有幂次相乘;
		{
			ans[poly[j].exp + exp] += poly[j].cof * cof;
		}
	}
	
	for (int i = 0; i <= 2000; i++)
	{
		if (ans[i] != 0.0)	number++;
	}

	printf ("%d", number);
	for (int i = 2000; i >= 0 ; i--)                                //依据题意从大到小输出
	{
		if (ans[i] != 0.0)
		{
			printf (" %d %.1f", i, ans[i]);        
		}
	}
	
	return 0;
}

3DMine E:\00-刚果(金)新矿业\1--2025年\03、验收数据\6月验收\6月交接数据\6月验收底图(已更新至0624交接).3ds, 3DMine String File file_version=3DMine_2009 1,7,0,1.00,1.00,1.00,0,0.5,0,Continuous,0624XK5交接 2,7,0,1.00,1.00,1.00,0,0.5,0,Continuous,0624XK9交接 3,7,0,1.00,1.00,1.00,0,0.5,0,Continuous,1 4,7,0,1.00,1.00,1.00,0,0.5,0,Continuous,范围线 5,7,0,1.00,1.00,1.00,0,0.5,0,Continuous,分界线 0,0,0,0,0,0,0,0, 3,8786919.71000,451915.69700,1308.81500,hp1474,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786863.21200,451888.23900,1290.13700,hp3051,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786835.08600,451964.11300,1287.88000,hp3105,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786859.85500,451993.22600,1299.00000,hp1547,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8787060.76762,452008.51951,1359.65897,G2031, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786935.94300,451877.72700,1300.95200,hp2697,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786855.12800,451878.27500,1287.72100,hp3082,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786936.66400,451840.95000,1289.51400,hp354,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786916.79600,451890.37000,1299.48200,hp2735,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786926.05500,453272.66600,1323.35700,d1814,\ 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786667.11900,452621.48900,1333.50500,2024.3.2原始地貌727, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786921.43500,451890.52300,1300.91400,hp2708,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786853.81600,451975.05700,1295.25500,hp1556,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786977.06100,451847.40200,1293.41500,hp84,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786865.68200,451965.13900,1298.65500,hp1558,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786950.45300,451895.76400,1310.28100,hp2660,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786889.20800,451997.80900,1309.83800,hp1510,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786964.69600,451849.94500,1291.37500,hp3408,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786939.91000,451898.51300,1309.79000,hp3009,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786854.29100,451962.89300,1296.18100,hp1543,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786917.05000,451892.91400,1300.21900,hp2717,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786930.05300,451907.43000,1309.99000,hp2663,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786975.39300,451834.29400,1291.90700,hp69,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786870.88500,451974.95800,1300.84000,hp1591,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786576.36000,451390.08400,1351.97400,A61, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786998.98200,451482.88200,1311.77700,B41, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786976.67000,451899.19900,1310.87100,hp3421,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786865.32900,451885.90700,1290.06000,hp3021,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786966.34500,451855.81900,1292.89700,hp3404,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786925.50900,451885.05300,1299.94600,hp2703,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786891.25600,451925.36800,1299.57700,hp2838,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786942.02200,451836.42300,1292.26600,hp358,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786929.36700,451882.47200,1300.29600,hp2710,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786965.16100,451858.90800,1293.99200,hp2707,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786415.29300,451540.29400,1349.02300,A24, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786421.40700,451503.67100,1350.90600,A219, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786834.49000,451932.04800,1290.99200,hp3058,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786846.90800,451907.62400,1292.45900,hp3027,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786864.40000,451958.72600,1297.30900,hp1567,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786962.80700,451883.30900,1306.70000,hp3008,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786965.01000,451877.93300,1303.28100,hp2998,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786964.74900,451851.60600,1291.53400,hp3407,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8787037.02500,453111.89400,1371.02600,tr554, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786959.94100,451843.44000,1290.05000,hp3414,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786414.56500,451557.34200,1347.38400,A21, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786599.87700,451358.69100,1351.49100,A179, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786962.17000,451861.77100,1295.79600,hp2701,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786979.67700,451829.03000,1292.25800,hp65,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786869.41000,451881.88600,1290.13600,hp3065,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786418.98200,451584.68800,1345.18300,A17, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786930.17800,451913.61800,1311.36000,hp2655,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786915.21600,451928.75500,1308.64600,hp1447,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786931.58200,451913.02400,1311.48100,hp2656,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786921.94100,451886.91300,1299.78600,hp2698,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786540.83700,451848.79500,1334.52900,a1189, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786904.09300,451948.41400,1307.91100,hp1480,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786559.43000,451824.01200,1337.35500,a639, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786947.68300,451890.34500,1307.72400,hp3000,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8787309.49800,452596.82400,1362.11500,tr24, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8787606.06800,450825.82500,1369.82700,806,\ 0,0.000,0.000,0.000,-1,,1.000,-1.00,-1.00,-1.00,7 3,8786732.20600,451317.25700,1356.22600,A153, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786831.97000,451287.56300,1361.72600,A132, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786938.53900,451840.64600,1290.45100,hp342,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786906.12200,451894.89000,1297.87000,hp2700,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8787383.11200,451642.36700,1264.52400,L67, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786733.10300,452629.35200,1337.96700,B4, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786408.40100,451523.09700,1350.08200,A223, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786976.63300,451838.18100,1292.79700,hp71,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786559.45600,451402.51600,1351.98200,A57, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786849.64200,451902.12600,1292.09200,hp3032,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786845.62500,451278.68700,1362.03200,A129, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786530.55600,451422.21100,1352.19800,A380, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786854.66900,451881.38000,1288.05400,hp3070,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786980.92700,451827.69100,1292.18100,hp64,Auto 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786419.53000,451528.02300,1350.06500,A26, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786856.92800,452675.55100,1396.93500,144, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786585.93500,451429.94200,1353.03700,A1009, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786411.19500,451622.35800,1342.60800,A241, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8787168.50600,453016.17800,1377.88800,tr511, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 3,8786715.12100,451333.56500,1355.87100,A90, 0,0.000,0.000,0.000,0,,1.000,-1.00,-1.00,-1.00,7 能否解释一下这个软件的数据结构是什么意思
最新发布
07-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值