PAT (Advanced Level) Practice 1002 A+B for Polynomials

【Problem Description】:
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 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.2

【解题思路】
考虑用链表来做这道题,用STL中的list容器,构建项结构体包含指数和系数,依次构建链表A和B,按降幂顺序排好,按规则加入和多项式C,注意三点:
① 输出要按照幂次从大到小顺序,格式上需要保留一位小数。
② 题目的输入和输出都是系数非零项。
③ 注意正负抵消的问题,两个多项式的某一个相同幂次的项刚好为相反数,相加之后就为 0 了所以不用输出。
【C++代码】

#include <iostream>
#include <list>
#include <algorithm>
#include <iomanip>
using namespace std;
struct term {	//多项式的项 
	int exp;	//指数 
	double coe;	//系数 
};
bool cmp(term a,term b) {	//按指数从大到小排序 
	return a.exp>b.exp;
}
int main() {
	list<term> A,B,C;
	int n,m;
	term t;
	cin>>n;
	while(n--) {	//构建多项式A 
		cin>>t.exp>>t.coe;
		A.push_back(t);
	}
	A.sort(cmp);
	cin>>m;
	while(m--) {	//构建多项式B 
		cin>>t.exp>>t.coe;
		B.push_back(t);
	}
	B.sort(cmp);
	while((!A.empty())&&(!B.empty())) {	//直到两个多项式链表中有一个为空 
		if(A.front().exp==B.front().exp) {	 
			t.exp=A.front().exp;
			t.coe=A.front().coe+B.front().coe;
			C.push_back(t);
			A.pop_front();
			B.pop_front();
		} else if(A.front().exp>B.front().exp) {
			C.push_back(A.front());
			A.pop_front();
		} else {
			C.push_back(B.front());
			B.pop_front();
		}
	}
	if(!A.empty()) {	//将不为空的链表的全部项加入和多项式 
		while(!A.empty()) {
			C.push_back(A.front());
			A.pop_front();
		}
	} else if(!B.empty()) {
		while(!B.empty()) {
			C.push_back(B.front());
			B.pop_front();
		}
	}
	int cnt=0;	//统计系数为0的项 
	for(list<term>::iterator it=C.begin(); it!=C.end(); it++) {
		if((*it).coe==0) {
			cnt++;
		}
	}
	C.sort(cmp);
	cout<<C.size()-cnt;	//项的个数 
	for(list<term>::iterator it=C.begin(); it!=C.end(); it++) {
		if((*it).coe!=0) {
			cout<<" "<<(*it).exp<<" "<<setiosflags(ios::fixed)<<setprecision(1)<<(*it).coe;//注意保留1位小数 
		}
	}
	cout<<endl;
	return 0;
}

【Java代码】

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		float polynomial[] = new float[1001];
		int n = scan.nextInt();
		for(int i = 0; i < n; i++) {
			int exp = scan.nextInt();
			polynomial[exp] = scan.nextFloat();
		}
		int m = scan.nextInt();
		for(int i = 0; i < m; i++) {
			int exp = scan.nextInt();
			polynomial[exp] += scan.nextFloat();
		}
		scan.close();
		int cnt = 0;
		for(int i = 0; i < polynomial.length; i++) {
			if(polynomial[i] != 0) {
				cnt++;
			}
		}
		System.out.print(cnt);
		for(int i = polynomial.length - 1; i >= 0 ; i--) {
			if(polynomial[i] != 0) {
				System.out.printf(" %d %.1f",i,polynomial[i]);
			}
		}
	}
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值