PAT 1002. A+B for Polynomials (25)

本文介绍了一种计算两个多项式相加的方法,并提供了一个具体的C++实现案例。输入包括两个多项式的系数和指数,输出为两个多项式相加后的结果。程序通过读取输入数据,进行多项式的加法运算,并按照指定格式输出结果。

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

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

Input

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

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.

Sample Input
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.
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int na, nb;
	float a[1005]= {0}, b[1005] = {0}, c[1005]={0};
	int count = 0, n;

	cin >> na;
	for (int i=0; i!=na; ++i) { //输入两组数据
		cin >> n;
	    cin >> a[n];
	}
	cin >> nb;
	for (int i=0; i!=nb; ++i) {
		cin >> n; 
	    cin >> b[n];
	}

	for (int i=0; i!=1005; ++i) {  //多项式相加并统计非零项
		c[i] = a[i] + b[i];
		if (c[i] != 0)
		   ++count;
	}
	cout << count;
	for (int i=1004; i>=0; --i)  {
        if (c[i] != 0)
	   cout << " " << i << " " 
		       << setiosflags(ios::fixed) << setprecision(1) << c[i];  //保留一位小数
	}
	cout << endl;
return 0;
}


### 关于 PAT 1002 A+B for Polynomials 的测试点 对于多项式相加的任务,在PAT 1002中,目标是找到两个多项式的和。每个多项式由若干项组成,而每一项则通过其指数和系数来定义[^1]。 #### 输入规格说明 每一个测试案例占据两行空间,每行描述了一个多项式的信息:首先是非零项的数量`K`,接着是以配对形式给出的一系列整数——先是一个表示指数的整数`Ni`紧接着一个浮点数作为该项的系数`aNi`。这里规定了`K`介于1至10之间;所有的指数满足条件`0 <= NK < ... < N2 < N1 <= 1000`[^4]。 #### 输出格式要求 最终输出应按照原始输入顺序排列各项,并且当某一项的结果恰好为零时,则不应将其列入输出之中。此外,所有非零项都应当遵循特定的小数位数显示规则,即保留一位小数[^5]。 #### 解决方案概述 一种有效的解决方案涉及使用数组来追踪各个指数所关联的总系数值。具体来说: - 创建长度至少为1001(考虑到最大可能的指数上限)的数组用于保存各次幂上的累积系数。 - 对第一个多项式的每一项更新此数组中的对应位置。 - 接着处理第二个多项式的数据并相应调整上述数组内的数值。 - 完成以上步骤之后,遍历整个数组寻找那些不等于零的位置,构建最终答案字符串的同时确保遵守必要的格式化标准[^3]。 ```java import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List<Term> resultTerms = new ArrayList<>(); Map<Integer, Double> termMap = new HashMap<>(); while (scanner.hasNext()) { int k = scanner.nextInt(); for (int i = 0; i < k; ++i) { int exponent = scanner.nextInt(); double coefficient = scanner.nextDouble(); termMap.merge(exponent, coefficient, Double::sum); } if (!scanner.hasNextLine().isEmpty()) break; } termMap.forEach((exp, coef) -> { if (Math.abs(coef) >= 1e-7) resultTerms.add(new Term(exp, coef)); }); Collections.sort(resultTerms, Comparator.comparingInt(Term::getExponent).reversed()); System.out.print(resultTerms.size()); for (Term t : resultTerms) { System.out.printf(" %d %.1f", t.getExponent(), t.getCoefficient()); } System.out.println(); } private static final class Term { private final int exponent; private final double coefficient; public Term(int exp, double coeff) { this.exponent = exp; this.coefficient = coeff; } public int getExponent() { return exponent; } public double getCoefficient() { return coefficient; } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值