PAT1002 A+B for Polynomials
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 (,) are the exponents and coefficients, respectively. It is given that 1,0.
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
可耻地没读懂题目。。这道题多项式加法也太不按(我的)常理出牌了吧(嘤嘤嘤
每个样例包含两行,每行第一个数表示几组(指数,系数)
那么输出也是这样。第一个数表示几组(指数,系数)
这边开始我不明白起来了。。为啥是3呢= =
原来这个统计的是指数的个数(跪了好吧)
相同指数的系数相加
这个输出还有点顺序→指数从大到小排列
分析完了我们就可以写了(呜呜呜)
#include<iostream> #include<iomanip> #include<stdlib.h> #include<stdio.h> using namespace std; int main() { int n, ex, cnt = 0; double co; double a[10000] = { 0 }; cin >> n; for (int i = 0; i < n; i++) { cin >> ex >> co; a[ex] += co; } cin >> n; for (int i = 0; i < n; i++) { cin >> ex >> co; a[ex] += co; } for (int i = 0; i < 1001; i++) { if (a[i] != 0) { cnt++; } } cout << cnt; for (int i = 1000; i >= 0; i--) { if (a[i] != 0) { printf(" %d %.1lf", i, a[i]); } } system("pause"); return 0; }
呜呜呜,说几个卡着的点
第一次错是因为系数是浮点型(嘤嘤嘤!数组也记得开浮点型嗷
第二个就是系数记得保留一位小数!!
上面是AC代码嘤嘤嘤溜了溜了