1002 A+B for Polynomials

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

 (1)17/25分代码:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

struct Node
{
    int exp;
    double coef;
    Node(int exp = 0,double coef = 0){
        this->exp = exp;
        this->coef = coef;
    }
//    operator < (const Node& a){
//        return exp > a.exp;
//    }
    bool operator == (const Node &a)
    {
        return exp == a.exp;
    }
};

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

vector<Node> a;
vector<Node> b;
vector<Node> ans;

int main()
{
    int k,exp;
    double coef;
    Node node;
    cin >> k;
    for(int i = 0; i < k; i++){
        cin >>exp>>coef;
        node = Node(exp,coef);
        vector<Node>::iterator it = find(a.begin(),a.end(),node);
        if(it!=a.end()){
            it->coef += node.coef;
        }
        else {
            a.push_back(node);
        }

    }

    cin >> k;
    for(int i = 0; i < k; i++){
        cin >>exp>>coef;
        node = Node(exp,coef);
        vector<Node>::iterator it = find(b.begin(),b.end(),node);
        if(it!=b.end()){
            it->coef += node.coef;
        }
        else {
            b.push_back(node);
        }
    }


    sort(a.begin(),a.end(),cmp);
    sort(b.begin(),b.end(),cmp);
    int i,j;
    for(i = 0, j = 0; i < a.size()&&j < b.size(); ){
        Node x = a[i];
        Node y = b[j];
        if(x.exp == y.exp){
            ans.push_back(Node(x.exp,x.coef+y.coef));
            i++;
            j++;
        }
        else if(x.exp > y.exp){
            ans.push_back(x);
            i++;
        }
        else {
            ans.push_back(y);
            j++;
        }
    }
    while(i < a.size()){
        ans.push_back(a[i++]);
    }
    while(j < b.size()){
        ans.push_back(b[j++]);
    }
    printf("%d",ans.size());
    for(int i = 0; i < ans.size(); i++){
        printf(" %d %.1lf",ans[i].exp, ans[i].coef);
    }
    return 0;
}
提交时间状态分数题目编译器耗时用户
2018/10/30 19:31:03

部分正确

171002C++ (g++)4 ms 
测试点结果耗时内存
0答案正确3 ms376KB
1答案正确2 ms384KB
2答案正确2 ms512KB
3答案错误4 ms512KB
4答案错误2 ms384KB
5答案错误2 ms384KB
6答案错误2 ms376KB

 

(2)写法2:25/25分

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

double p[1001];

int main()
{
    int k,exp;
    double coef;
    cin >> k;
    for(int i = 0; i < k; i++){
        cin >>exp>>coef;
        p[exp] += coef;
    }
    cin >> k;
    for(int i = 0; i < k; i++){
        cin >>exp>>coef;
        p[exp] += coef;
    }
    int cnt = 0;
    for(int i = 0; i < 1001; i++){
        if(p[i]!=0) cnt++;
    }
    printf("%d",cnt);
    for(int i = 1001; i >= 0; i--){
        if(p[i]!=0){
            printf(" %d %.1lf",i,p[i]);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值