1002. A+B for Polynomials (25)

来源:互联网 发布:手游淘宝交易平台 编辑:程序博客网 时间:2024/05/16 05:01

1002. A+B for Polynomials (25)

Question
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


这道题目的思路完全是按着人的直观想法来的。
用数组在表示一个多项式,每一个index表示次数,其中的value就是系数。所以要一个1001大小的、初始化为0的数组。
然后把第一个不等式的系数按照次数索引加进去,再把第二个不等式的系数按照次数索引加进去。
最后数一下数组中不为0的个数,就是新多项式的K,然后把数组倒着输出不为0的值即可啦!

最直观的代码

#include <iostream>using namespace std;const int MAX_EXP = 1001;int main(int argc, const char * argv[]) {    double p1[MAX_EXP] = {0}, p2[MAX_EXP] = {0}, p3[MAX_EXP] = {0};    int K1, K2, exp;    double coefficient;    cin >> K1;    for (int i = 0; i < K1; ++i) {        cin >> exp >> coefficient;        p1[exp] = coefficient;    }    cin >> K2;    for (int i = 0; i < K2; ++i) {        cin >> exp >> coefficient;        p2[exp] = coefficient;    }    int K3 = 0;    for (int i = 0; i < MAX_EXP; ++i) {        p3[i] = p1[i] + p2[i];        if (p3[i] != 0) {            ++K3;        }    }    cout << K3;    for (int i = MAX_EXP - 1; i >= 0; --i) {        if (p3[i] != 0) {            //这里一定要注意!输出的格式!保留1位小数!            printf(" %d %.1lf", i, p3[i]);        }    }    return 0;}

稍微改进一点儿

#include <iostream>using namespace std;const int MAX_EXP = 1001;int main(int argc, const char * argv[]) {    double p[MAX_EXP] = {0};    int exp = 0, K = 0 ;    cin >> K;    for (int i = 0; i < K; i++) {        cin >> exp;        cin >> p[exp];    }     cin >> K;    for (int i = 0; i < K ;i++) {        double temp = 0;        cin >> exp >> temp;        p[exp] += temp;    }    K = 0;    for (int i = 0; i < MAX_EXP; i++) {        if (p[i] != 0) {            K++;        }    }    cout << K;    for (int i = MAX_EXP - 1; i >= 0; i--) {        if (p[i] != 0) {            //这里一定要注意!输出的格式!保留1位小数!            printf(" %d %.1lf", i, p[i]);        }    }    return 0;}

别人的解法

#include<iostream>#include <vector>#include <algorithm>#include <iomanip>using namespace std;bool  compare(pair<int,float> a,pair<int,float> b) {    return a.first > b.first;}int main() {    int M,N;    vector<pair<int,float> >  vec;    vector<pair<int,float> >  vec1;    vector<pair<int,float> >::iterator it,it1;    float exp=0;    float coe=0;    cin>>M;    while(M--) {        cin>>exp>>coe;        vec.push_back(make_pair(exp,coe));    }    cin>>N;    while(N--) {        cin>>exp>>coe;        vec.push_back(make_pair(exp,coe));    }    sort(vec.begin(),vec.end(),compare);    for (it=vec.begin();it!=vec.end();it=it1){        for (it1=it+1; it1!=vec.end()                 && (*it).first == (*it1).first; it1++) {            (*it).second += (*it1).second;        }        if ((*it).second !=0) {            vec1.push_back(*it);        }    }    cout<<vec1.size();    for (it=vec1.begin();it!=vec1.end();it++) {        cout<<" "<<(*it).first            <<" "<<fixed<<setprecision(1)<<(*it).second;    }    cout<<endl;    return 0;}
0 0
原创粉丝点击