1002. A+B for Polynomials (25)

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 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (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



问题是要解决一个多项式的加法,这次吸取了之前的经验,先思考好才开始写,但是过程还是不是很顺利。
1.没有认真读题,在输入中规定了It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.然后说输出格式跟输入一样,也就是N是依次递减的,理解错题意,花费了时间写了一个更新数组。
2.Please be accurate to 1 decimal place. 这句话是精确到小数点一位,输出格式没写对
3.最重要的一点,在看别人的代码的时候才发现,当一个项为0的时候就不需要输出了!

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <map>


using namespace std;

map<int,double> m;

int main()
{
    int k1,k2;
    cin>>k1;
    int total=0;
    int sum[11];
    int j;
    for( j=0;j<k1;j++){
            int n;
            double an;
            cin>>n>>an;
            m[n]=an;
            sum[j]=n;
            total++;
    }
    cin>>k2;
    for(int i=0;i<k2;i++){
        int n;
        double an;
        cin>>n>>an;
        if(m.count(n))  m[n]=m[n]+an;
        else{
            m[n]=an;
            total++;
            sum[j+i]=n;
        }
    }

    int temp=0;
    for(int a=0;a<total;a++){
        for(int b=a;b<total;b++){
            if(sum[a]<sum[b]){
              temp=sum[b];
              sum[b]=sum[a];
              sum[a]=temp;
            }
        }
    }
    int n=total;
    for(int i=0;i<total;i++){
            if(m[sum[i]]==0){
                n--;
            }
    }

    cout<<n;
    for(int i=0;i<total;i++){
        if(m[sum[i]])
        printf(" %d %.1lf",sum[i],m[sum[i]]);
    }

    return 0;
}

 




 



posted @ 2016-11-10 19:53  Mel年  阅读(329)  评论(0编辑  收藏  举报