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
- 收藏
- 分享
- 文章举报
- Pat(Advanced Level)Practice--1002(A+B For Polynomials)
- PAT (Advanced Level) Practice 1002 A+B for Polynomials
- 1002 A+B for Polynomials (25 分)[PAT (Advanced Level) Practice]
- PAT(Advanced level) 1002 A+B for Polynomials(25)
- PAT(Advanced Level) 1002 - A+B for Polynomials(水题)
- 编程题目:PAT(Advanced Level) Practice 1002. A+B for Polynomials (25)
- PAT (Advanced Level) Practise 1002 A+B for Polynomials
- PAT (Advanced Level) Practise 1002 A+B for Polynomials (25)
- PAT (Advanced Level) Practise 1002 A+B for Polynomials (25)
- Pat(Advanced Level)Practice--1039(Course List for Student)
- PAT (Advanced Level) 1002. A+B for Polynomials (25)
- 浙大PAT (Advanced Level) Practise 1002. A+B for Polynomials (25)
- Pat(Advanced Level)Practice--1047(Student List for Course)
- PAT (Advanced Level) Practise 1002. A+B for Polynomials (25)
- PAT(Advanced Level)1002. A+B for Polynomials (25)
- 【PAT】【Advanced Level】1002. A+B for Polynomials (25)
- 【PAT Advanced Level】1002. A+B for Polynomials (25)
- (PAT Advanced Level) 1002.A+B for Polynomials(多项式模拟) C++
- Pat(Advanced Level)Practice--1009(Product of Polynomials)
- PAT (Advanced Level) 1002. A+B for Polynomials (25) 解题报告