1002 A+B for Polynomials(多项式的A+B)

该博客介绍如何计算两个多项式的和,其中输入格式为K N1 aN1 N2 aN2 … NK aNK,输出同样格式,要求精确到1位小数。文章详细阐述了解题思路和注意事项,并提供了一组示例数据。

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

problem description

  This time, you are supposed to find A+B where A and B are two polynomials.

题目描述

  这一次,你应该找到A,B,其中A和B是两个多项式。

Input Specification:

  Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:
K N​1​​ a​N​1​​​​ N​2​​ a​N2​​ … N​K​​ a​N​K​​
where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1 ≤ K ≤ 10,0 ≤ N​K < ⋯ < N​2 < N​1 ≤ 1000.

输入格式:

  每个输入文件包含一个测试用例。每一种情况占 2 行,每行包含多项式信息:K N​1 ​​a​N​1 ​N​2 ​​a​N2​​ … N​K​​ a​N​K​​,其中 K 是多项式中的非零项数, N​i ​​和 a​N​i​ ( i=1,2,⋯,K)分别是指数和系数。给出了 1 ≤ K ≤ 10 , 0 ≤ N​K < ⋯ < N​2 < N​1 ≤ 1000 。

Output Specification:

  For each test case you should output the sum 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 to 1 decimal place.

输出格式:

  对于每个测试用例,您应该在一行中输出A和B的和,其格式与输入相同。请注意,每一行的末尾必须没有多余的空格。请精确到小数点1位。

Sample Input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
输入样例:
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
输出样例:
3 2 1.5 1 2.9 0 3.2

思路
  步骤1:令 double 型数组 p[max_n] 表示多项式,其中 p[n] 表示幂次为 n 的项的系数,初值为 0 。令 int 型变量 count 表示系数非零项的个数,初值为 0 。
  步骤2:先按输入格式读入第一个多项式,再读入第二个多项式,并把对应系数直接加到第一个多项式上。
  步骤3:计算非零系数项的个数 count 并输出,然后按格式输出该多项式。

注意
  ① 输出要按照幂次从大到小的顺序,格式上需要保留一位小数。
  ② 题目的输入和输出都是系数非零项。
  ③ 有些人会选择在读入的过程中就使用 count 计数,这里要注意正负抵消的问题:两个多项式的某一个相同幂次的项刚好为相反数,相加之后就为 0 了。因此采用下面这个写法的要注意 count 必须在两项相加之后再判断是否自减。

scanf("%d", &k)</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值