PAT--1002 A+B for Polynomials (25 分)

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 N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N​K​​<⋯<N​2​​<N​1​​≤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

先上我的粗暴想法:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

const int maxk = 20;
struct Node
{
    double coe;
    int exp;
} a[maxk],b[maxk],c[maxk];

bool cmp(Node a,Node b)
{
    return a.exp>b.exp;
}

int main()
{
    int k1,k2;
    cin>>k1;
    int e;
    double co;
    for(int i=0; i<k1; i++)
    {
        //cin>>e>>c;
        //a[i].exp = e;
        //a[i].coe = c;
        cin>>a[i].exp>>a[i].coe;
    }
    sort(a,a+k1,cmp);
    cin>>k2;
    for(int i=0; i<k2; i++)
    {
        //cin>>e>>c;
        //b[i].exp = e;
        //b[i].coe = c;
        cin>>b[i].exp>>b[i].coe;
    }
    sort(b,b+k2,cmp);
    int flag = 0;
    int i=0,j=0;
    while(i!=k1 && j!=k2)
    {
        if(a[i].exp>b[j].exp)
        {
            c[flag++] = a[i];
            i++;
        }
        else if(a[i].exp==b[j].exp)
        {
            c[flag].coe = a[i].coe+b[j].coe;
            c[flag].exp = a[i].exp;
            i++;
            j++;
            flag++;
        }
        else if(b[i].exp>a[j].exp)
        {
            c[flag++] = b[j];
            j++;
        }
    }

    while(i!=k1)
    {
        c[flag++] = a[i];
        i++;
    }

    while(j!=k2)
    {
        c[flag++] = b[j];
        j++;
    }

    cout<<flag;

    for(int s=0; s<flag; s++)
    {
        cout<<" "<<c[s].exp<<" ";
        printf("%.1f",c[s].coe);
    }
    cout<<endl;
    //cout << "Hello world!" << endl;
    return 0;
}

本来觉得我的思路及其正确,怎么都通不过是格式问题,结果后来发现都是bug:

  1. 题目可以输入重复的项,如果是输入重复的项,那这个代码就会将其作为两个多项式,没有合并
  2. 如果输入的项相加之后为0,应该不输出,但是这个代码没有判断就会直接输出

参考大佬代码:

#include<iostream>
using namespace std;
const int maxn = 1e3 + 10;
double a[maxn], b[maxn];
int k;
void shuru(double a[])
{
    scanf("%d", &k);
    for (int i = 0; i < k; i++)
    {
        int exp;
        double coef;
        scanf("%d%lf", &exp, &coef);
        a[exp] = coef;
    }
}
int main()
{
    shuru(a);
    shuru(b);
    int cnt = 0;
    for (int i = 0; i < maxn; i++)
    {
        a[i] += b[i];
        if (a[i])cnt++;
    }
    printf("%d", cnt);
    for (int i = maxn; i >= 0; i--)
    {
        if (a[i])printf(" %d %.1f", i, a[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jackson_j/article/details/100088253