1002 A+B for Polynomials (25 分)

题目链接:

https://pintia.cn/problem-sets/994805342720868352/problems/994805526272000000

题目描述:

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:

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

题目大意:

给两个多项式,要求两者相加求系数。

思路:

有一说一,这题很水,两者相加即可,需要注意的是系数为0时该项自动省去。

 

AC代码:

#include <iostream>
#include <cstring>

using namespace std;

double a[1005];
int num[1005];

int main()
{
    cout.precision(1);
    cout.setf(ios::fixed);    //小数点后保留一位
    memset(a,0,sizeof(a));
    memset(num,0,sizeof(num));     //将两个数组初始化为0
    int n,temp1;                  //temp1用于记录幂数
    double temp2;                 //temp2用于记录系数
    cin >> n;
    for(int i=1; i<=n; i++)       //输入第一个多项式
    {
        cin >> temp1 >> temp2;
        a[temp1] = temp2;
    }
    cin >> n;
    for(int i=1;i<=n;i++)       //输入第二个多项式
    {
        cin >> temp1 >> temp2;
        a[temp1] += temp2;
    }
    int sum=0;                 //用于记录系数不为0的个数
    for(int i=1000; i>=0; i--)    //最大1000次幂
    {
        if(a[i]!=0) num[++sum] = i;
    }
    cout << sum;
    for(int i=1;i<=sum;i++)
        cout << ' '  << num[i] << ' ' << a[num[i]];
    return 0;
}
View Code

 

posted @ 2019-11-19 21:19  abszse  阅读(127)  评论(0)    收藏  举报