【PAT】A1002 A+B for Polynomials (25)-PAT甲级真题

A1002 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

【polynomial:多项式的 nonzero term:非零项 decimal:十进制的

exponents:指数 exponential:指数的 coefficient:系数 respectively:分别地,依次地

1、自解(哈希表)

#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

void AB_polynomial(vector<float>& s1, vector<float>& s2)
{
    unordered_map<int, float> m;
    for (int i = 1; i <= s1[0]*2; i += 2)
    {
        m.insert(make_pair(s1[i], s1[i + 1]));
    }
    for (int j = 1; j <= s2[0]*2; j += 2)
    {
        if (m.count(s2[j])) { m[s2[j]] += s2[j + 1]; }
        else{ m.insert(make_pair(s2[j], s2[j + 1])); }
    }
    int len = m.size();
    cout << len << ' ';
    for (auto it = m.begin();it!=m.end();it)
    {
        cout << it->first <<' ' << it->second;
        it++;
        if (it != m.end())cout << ' ';//最后没有多余的空格
    }
}

int main()
{
    vector<float>s1,s2;
    float a;
    cin >> a; s1.push_back(a);
    for (int i = 1; i <= s1[0]*2; i++)
    {
        cin >> a; s1.push_back(a);
    }
    cin >> a; s2.push_back(a);
    for (int i = 1; i <= s2[0] * 2; i++)
    {
        cin >> a; s2.push_back(a);
    }
    /*s1 = {2,1,2.4,0,3.2};
    s2 = { 2,2,1.5,1,0.5 };*/
    AB_polynomial(s1, s2);
}

Conclusion:

1、对于unordered_map,在插入键值对时要用:

m.insert(make_pair(a,b));

2、查找键值:

m.count(a)!=0//表示m表中存在a为关键字的数,这个数是m[a]

3、可以用迭代器遍历哈希表:

for(auto it = m.begin();it!=m.end();it++)
{
    cout<<it.first<<it.second;
}

4、对于vector的赋值方法有两种:

(1)直接赋值

vector<int> s;
s = {1,2,3,4,5};

(2)cin函数接收元素赋值:

int a;
cin>>a;
s.push_back(a);//将a赋值给s

2、柳神方法

直接用数组记录这些值....引发了我对hash和基础数组的区别的思考.......

优点:hash最大的方便在于可以快速查找元素,所以对于需要搜索大量数据的情况,hash的复杂度还是更低的!

缺点:需要额外创建一个hash表并且输入元素,费时间空间

总结:可以用hash但没那么必要!柳神yyds555

It is given that 1 <= K <= 10,0 <= NK < … < N2 < N1 <=1000.所以直接设置c1[1001]数组就好啦

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
    float c[1001] = { 0 };
    int a; cin >> a;
    int t; float num;
    for (int i = 0; i < a; i++)
    {
        scanf_s("%d %f", &t, &num);
        c[t] += num;
    }
    int b; cin >> b;
    for (int j = 0; j <  b; j++)
    {
        scanf_s("%d %f", &t, &num);
        c[t] += num;
    }
    int count = 0;
    for (int i = 0; i < 1001; i++)
    {
        if (c[i])count++;
    }
    cout << count;
    for (int i = 1000; i >= 0; i--)
    {
        if (c[i] != 0.0)printf(" %d %.1f", i, c[i]);
    }
}

【补充:找到了一个用hash的致命缺点:输出的结果具有随机性,不能排序!虽然题目里面也没有明确要求,但是和范例的output就是不一样......想要修改,但是在hash里面排序简直太违背它的魅力了TVT放弃啦 柳神的数组方法就ok】

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值