1002 A+B for Polynomials (25分)_29行代码AC
立志用最少的代碼做最高效的表達
PAT甲級最優題解——>傳送門
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:
KN?1a?N1N2aN2…N?KaNKK N?1 a?N1 N2 aN2 … N?K aNKKN?1a?N1N2aN2…N?KaNK where K is the number of nonzero terms in the polynomial, N?iN?iN?i and aNi??(i=1,2,?,K)aNi?? (i=1,2,?,K)aNi??(i=1,2,?,K) are the exponents and coefficients, respectively. It is given that 1≤K≤101≤K≤101≤K≤10,0≤NK<?<N?2<N1≤10000≤NK<?<N?2<N1≤10000≤NK<?<N?2<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
注意要點:
1、如果相加后總多項式為0,則輸出0
2、如果某一項為0,則不輸出
#include<bits/stdc++.h> using namespace std; double s[1010]; int main() {int k1; cin >> k1; while(k1--) {int x; double d; cin >> x >> d;s[x] += d;}cin >> k1; while(k1--) {int x; double d; cin >> x >> d;s[x] += d;}int num = 0;for(int i = 1009; i >= 0; i--) if(s[i] != 0) num++; if(num == 0) cout << 0;else {cout << num;for(int i = 1009; i >= 0; i--) {if(s[i] != 0) printf(" %d %.1lf", i, s[i]);}}putchar('\n');return 0; }
耗時
總結
以上是生活随笔為你收集整理的1002 A+B for Polynomials (25分)_29行代码AC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1001 A+B Format (20分
- 下一篇: 【简单数论】H - A^X mod P_