1002. A+B for Polynomials(25分)

简介: 1002. A+B for Polynomials(25分)

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 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 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
结尾无空行
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct node
{
    int z;
    float x;
};
bool compare(node A, node B)
{
    return A.z > B.z;
}
int main()
{
    vector<node> A, B, C;
    node tmp;
    int a, b;
    cin >> a;
    for (int i = 0; i < a; i++)
    {
        cin >> tmp.z >> tmp.x;
        A.push_back(tmp);
    }
    cin >> b;
    for (int i = 0; i < b; i++)
    {
        cin >> tmp.z >> tmp.x;
        B.push_back(tmp);
    }
    // 排序
    sort(A.begin(), A.end(), compare);
    sort(B.begin(), B.end(), compare);
    // 双指针法
    int i, j;
    for (i = 0, j = 0; i < a && j < b;)
    {
        if (A[i].z > B[j].z)
        {
            tmp = A[i++];
            C.push_back(tmp);
        }
        else if (A[i].z < B[j].z)
        {
            tmp = B[j++];
            C.push_back(tmp);
        }
        else
        {
            tmp.z = A[i].z;
            tmp.x = A[i++].x + B[j++].x;
            // 系数为零就抵消了
            if (tmp.x)
            {
            C.push_back(tmp);
            }
        }
    }
    // 余值按序插入,2个while只有一个会被执行
    while (i < a)
    {
        tmp = A[i++];
        C.push_back(tmp);
    }
    while (j < b)
    {
        tmp = B[j++];
        C.push_back(tmp);
    }
    // 输出
    cout << C.size();
    for (node it : C)
    {
        printf(" %d %.1f", it.z, it.x);
    }
    system("pause");
    return 0;
}


受最近数据结构的影响,思维还没转变过来。想到的是传统的结构体解法,合并多项式借用了归并排序的思想,先将2个多项式降序排序,然后双指针法合并。卡壳的主要原因是没有考虑多项式相加为零要剔除的情况。

#include <iostream>
using namespace std;
int main()
{
    double N[1005] = {0};
    for (int i = 0; i < 2; i++)
    {
        int len;
        cin >> len;
        for (int k = 0; k < len; k++)
        {
            int z;
            double x;
            cin >> z >> x;
            N[z] += x;
        }
    }
    int count = 0;
    for (int i = 0; i <= 1000; i++)
    {
        if (N[i] != 0)
        {
            count++;
        }
    }
    cout << count;
    for (int i = 1000; i >= 0; i--)
    {
        if (N[i] != 0)
        {
            printf(" %d %.1f", i, N[i]);
        }
    }
    return 0;
}


数组下标表示指数,数组元素表示系数。简单明了。

目录
相关文章
|
存储 Java 索引
十分钟教你学会JAVA中的学生管理系统
十分钟教你学会JAVA中的学生管理系统
382 2
|
机器学习/深度学习 自然语言处理 数据可视化
基于Python+词云图+情感分析对某东上完美日记的用户评论分析
基于Python+词云图+情感分析对某东上完美日记的用户评论分析
854 0
基于Python+词云图+情感分析对某东上完美日记的用户评论分析
|
SQL Java 关系型数据库
SpringBoot从入门到精通(三十)如何使用JdbcTemplate操作数据库?
前面介绍了Mybatis数据持久化框架,Mybatis虽然功能强大,但是,使用起来还是比较复杂的。所以接下来介绍一个简单的数据持久化框架——JdbcTemplate。
10351 2
SpringBoot从入门到精通(三十)如何使用JdbcTemplate操作数据库?
|
8月前
|
人工智能 JavaScript 前端开发
【最佳实践系列】AI程序员让我变成全栈:基于阿里云百炼DeepSeek的跨语言公告系统实战
本文介绍了如何在Java开发中通过跨语言编程,利用阿里云百炼服务平台的DeepSeek大模型生成公告内容,并将其嵌入前端页面。
532 10
|
NoSQL MongoDB 数据库
MongoDB最新版本是什么?
【6月更文挑战第8天】MongoDB最新版本是什么?
698 6
|
11月前
|
容器
【HarmonyOS】HMRouter使用详解(一)环境配置
在项目中使用官方推荐的Navigation时,需要在所有的页面上都添加一层NavDestination,在代码阅读上会增加多个层级,而且还要在主页面设置对应名字的跳转等问题,配置起来比较繁琐。看到大佬开发的HMRouter使用起来方便简洁,因此,写下这篇文章记录HMRouter的使用。
576 8
【HarmonyOS】HMRouter使用详解(一)环境配置
|
机器学习/深度学习 XML 人工智能
Prompt进阶系列5:LangGPT(提示链Prompt Chain)--提升模型鲁棒性
Prompt进阶系列5:LangGPT(提示链Prompt Chain)--提升模型鲁棒性
Prompt进阶系列5:LangGPT(提示链Prompt Chain)--提升模型鲁棒性
|
人工智能 分布式计算 DataWorks
连续四年!阿里云领跑中国公有云大数据平台
近日,国际数据公司(IDC)发布《中国大数据平台市场份额,2023:数智融合时代的真正到来》报告——2023年中国大数据平台公有云服务市场规模达72.2亿元人民币,其中阿里巴巴市场份额保持领先,占比达40.2%,连续四年排名第一。
620 12
|
XML SQL Java
Spring 中声明式事务和编程式事务的使用
Spring 中声明式事务和编程式事务的使用
267 0
|
数据可视化 前端开发 JavaScript
【Echarts大屏】大客户银行可视化大屏(附源码一键复制)
【Echarts大屏】大客户银行可视化大屏(附源码一键复制)

热门文章

最新文章