1002 A+B for Polynomials (25分) 测试用例+犯了所有可能犯的错误

24 篇文章 0 订阅
17 篇文章 0 订阅

原题

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:在这里插入图片描述

where K is the number of nonzero terms in the polynomial, 在这里插入图片描述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

很烂的通过代码

好的代码指路:https://www.cnblogs.com/baichangfu/p/7168221.html
用数组下标代表指数,数组内元素存储系数

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<list>
#include<string>
#include<map>
#include<vector>
#include<iomanip>
using namespace std;

bool gt(const pair<int,double> &a, const pair<int,int> &b) {
    return a.first>b.first;
}

int main() {
    cout.setf(ios::fixed);
    vector<pair<int,double>> p;

    int k1,k2;

    cin>>k1;
    int i;
    pair<int,double> tmp;
    for(i=0;i<k1;i++) {
        cin>>tmp.first>>tmp.second;
        int flag=1,sz=p.size();
        for(int j=0;j<sz;j++) {
            if(tmp.first==p[j].first) {
                p[j].second+=tmp.second;
                flag=0;
                break;
            }
        }
        if(flag)   p.push_back(tmp);
    }
    cin>>k2;

    int zero=0;
    for(i=0;i<k2;i++) {
        cin>>tmp.first>>tmp.second;

        for(int j=0;j<k1;j++) {
            if(p[j].first==tmp.first) {
//                cout<<"?_3"<<endl;
                p[j].second+=tmp.second;
                if(p[j].second==0) zero++;
//                cout<<"tmp.first"<<' '<<"tmp.second"<<endl;
//                cout<<tmp.first<<' '<<tmp.second<<endl;
//                cout<<"p[j].first"<<' '<<"p[j].second"<<endl;
//                cout<<p[j].first<<' '<<p[j].second<<endl;
                break;
            } else if(j==k1-1) {
                p.push_back(tmp);
            }
        }
    }
    sort(p.begin(), p.end(),gt);
    cout<<p.size()-zero;
    for(i=0; i<p.size()-1;i++) {
        if(p[i].second==0) continue;
        else cout<<' '<<p[i].first<<' '<<setprecision(1)<<p[i].second;
    }
    if(p[i].second!=0) cout<<' '<<p[i].first<<' '<<setprecision(1)<<p[i].second;
}

测试用例&需要注意

  1. 同一个多项式里,可能有没有合并的、指数相同的项
    test(有两个指数为2的项)
    3 2 2.5 2 2.4 0 3.2
    2 2 -2.5 1 0.5
    result
    3 2 2.4 1 0.5 0 3.2

  2. 注意题目要求Please be accurate to 1 decimal place,需要一位小数

    • 我在这一步时使用了setprecision(1),但这个函需要搭配cout.setf(ios::fixed)使用才意味着小数点后一位有效数字,否则就成了保留一位有效数字
    • 精度控制语句具体看这里:setprecision、fixed、showpoint的用法总结
      test
      10 2 2.5 2 2.4 0 3.2 2 2.5 2 2.4 0 3.2 2 2.5 2 2.4 0 3.2 0 4
      10 2 -2.5 1 0.5 2 -2.5 1 0.5 2 -2.5 1 0.5 2.5 2 2.4 0 3.2 0 4
      result
      3 2 10.1 1 1.5 0 20.8
  3. 如果结果系数全为0,也就是结果是0,那只输出项数0
    test
    1 0 -3.2
    1 0 3.2
    result
    0

我的全部测试

testresult
2 1 2.4 0 3.2
2 2 1.5 1 0.5
3 2 1.5 1 2.9 0 3.2
3 2 2.5 1 2.4 0 3.2
2 2 1.5 1 0.5
3 2 4.0 1 2.9 0 3.2
1 0 3.2
1 0 -3.2
0
1 0 3.2
1 0 7.89
1 0 6.1
3 2 2.5 1 2.4 0 3.2
2 2 -2.5 1 0.5
2 1 2.9 0 3.2
3 2 2.5 2 2.4 0 3.2
2 2 -2.5 1 0.5
3 2 2.4 1 0.5 0 3.2
10 2 2.5 2 2.4 0 3.2 2 2.5 2 2.4 0 3.2 2 2.5 2 2.4 0 3.2 0 4
10 2 -2.5 1 0.5 2 -2.5 1 0.5 2 -2.5 1 0.5 2.5 2 2.4 0 3.2 0 4
3 2 10.1 1 1.5 0 20.8
  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值