POJ 多项式加法

题解:

        采用顺序表。考虑到题目中没有规定指数上界,为避免RE,拟不采用数组。参考了http://blog.csdn.net/inlovecy/article/details/15208473后,最终采用map。

源码:

#include <iostream>
#include <map>
using namespace std;

int main()
{
    int n;
    cin >> n;
    while (n--) {
        int exp = 0, t1, t2, c = 2;
        bool flg = false;
        map<int, int>ep;
        while (c--) 
            while (cin >> t1 >> t2) {
                if (t2 < 0) break;
                if (ep.find(t2) != ep.end())ep[t2] += t1;
                else ep.insert(make_pair(t2, t1));
            }
        for (map<int, int>::reverse_iterator it = ep.rbegin(); it != ep.rend(); it++) {
            if (it->second == 0)continue;
            cout << "[ " << it->second << " " << it->first << " ] ";
            flg = true;
        }
        if(flg) cout << endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Jeffrey-Y/p/9279898.html