1002 A+B for Polynomials (25)(25 point(s))

时间:2022-03-21 05:49:17

problem

1002 A+B for Polynomials (25)(25 point(s))
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 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (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.2

anwser

#include<iostream>
#include<stdio.h>
#include<iomanip>
#include<cstring> int main()
{
freopen("test.txt", "r",stdin);
float n[1001], n1[1001], n2[1001];
memset(n, 0, 1001*sizeof(float));
memset(n1, 0, 1001*sizeof(float));
memset(n2, 0, 1001*sizeof(float));
int a, b; std::cin>>a;
for(int i = 0; i < a; i++){
int temp1;
float temp2;
std::cin>>temp1>>temp2;
n1[temp1] = temp2;
} std::cin>>b;
for(int i = 0; i < b; i++){
int temp1;
float temp2;
std::cin>>temp1>>temp2;
n2[temp1] = temp2;
} int c = 0;
for(int i = 0; i < 1001; i++){
n[i] = n1[i] + n2[i];
// std::cout<<n[i]<<i<<std::endl;
if(n[i] != 0) c++;
}
std::cout<<c;
for(int i = 1000; i >= 0; i--){
if(n[i] != 0) {
std::cout<<" "<<i<<" ";
std::cout<<std::fixed<<std::setprecision(1)<<n[i];
// printf("%.1f", n[i]);
}
}
return 0;
} /*
2 1 2.4 0 3.2
2 2 1.5 1 0.5
*/

experience

  • 注意头文件名
  • 注意边界条件以及输出格式

单词复习:

  • polynomials 多项式
  • exponents 范例,指数
  • coefficients 系数
  • respectively 分别的
  • accurate 精确的
  • decimal 小数