首页 > 其他分享 >PAT Advanced 1002. A+B for Polynomials

PAT Advanced 1002. A+B for Polynomials

时间:2023-04-27 10:22:58浏览次数:48  
标签:PAT res Polynomials tempDouble Limit tempInt 1002 Advanced

PAT Advanced 1002. A+B for Polynomials

1. Problem Description:

This time, you are supposed to find \(A+B\) where \(A\) and \(B\) are two polynomials.

2. 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\).

3. 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.

4. Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

5. Sample Output:

3 2 1.5 1 2.9 0 3.2

6. Performance Limit:

Code Size Limit
16 KB
Time Limit
400 ms
Memory Limit
64 MB

思路:

定义map<int, double>类型变量res存储两个多项式的和,多项式每一项的指数作为key,累加其系数即可,最后倒序遍历res进行输出。这里第一次提交时testpoint3,4,5,6报wrong answer,排查后发现忽略了系数为负数的情况,输出前需要将res中系数为0的元素进行删除,修改后AC。

参考大佬题解:1002. A+B for Polynomials (25)-PAT甲级真题_柳婼的博客-CSDN博客 ,其实根据指数 \(N_i\) 的上限维护一个数组即可,不过这里用map容器不用考虑内存分配还是很爽的。

My Code & Result:

#include <iostream>
#include <map>

using namespace std;

// first submit testpoint3, 4, 5, 6 wrong answer
int main(void)
{
    map<int, double> res;
    int termCount=0, tempInt=0;
    double tempDouble=0.0;
    int i=0; //iterator

    cin >> termCount;
    for(i=0; i<termCount; ++i)
    {
        cin >> tempInt >> tempDouble;
        res[tempInt] += tempDouble;
    }

    cin >> termCount;
    for(i=0; i<termCount; ++i)
    {
        cin >> tempInt >> tempDouble;
        res[tempInt] += tempDouble;
    }

    // coeffinet may be negative, cause some term in res be zero
    for(auto it=res.begin(); it!=res.end(); ) // erase the zero term, this fixed testpoint3, 4, 5, 6
    {
        if(it->second == 0)
        {
            it = res.erase(it);
        }
        else
        {
            ++it;
        }
    }
    
    
    cout << res.size();
    for(auto it=res.rbegin(); it!=res.rend(); ++it)
    {
        //cout << ' ' << it->first << ' ' << it->second;
        printf(" %d %.1lf", it->first, it->second);
    }
    cout << endl;

    return 0;
}
Compiler
C++ (g++)
Memory
440 / 65536 KB
Time
4 / 400 ms

标签:PAT,res,Polynomials,tempDouble,Limit,tempInt,1002,Advanced
From: https://www.cnblogs.com/tacticKing/p/17358183.html

相关文章

  • 设计模式(18)-Command Pattern
    一、 命令(Command)模式命令(Command)模式属于对象的行为模式【GOF95】。命令模式又称为行动(Action)模式或交易(Transaction)模式。命令模式把一个请求或者操作封装到一个对象中。命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。......
  • 第三十一章:XPath
    学习要点:1.IE中的XPath2.W3C中的XPath3.XPath跨浏览器兼容XPath是一种节点查找手段,对比之前使用标准DOM去查找XML中的节点方式,大大降低了查找难度,方便开发者使用。但是,DOM3级以前的标准并没有就XPath做出规范;直到DOM3在首次推荐到标准规范行列。大部分浏览器实现了......
  • Correct the classpath of your application so that it contains a single, compatib
    1.背景有时候引入包有冲突,比如在Maven项目中的不同模块多次重复引入等这里遇到的问题是重复映入了如下包:<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.0-RELEASE</vers......
  • Advanced Installer添加快捷方式和卸载功能
    依次点击左侧“资源”中的“文件和文件夹”选中“应用程序快捷方式文件夹”,在右侧空白处右键-新建快捷方式在弹出的对话框中,选择需要创建快捷方式的文件,点击确定按钮,此时成功添加了一个快捷方式,然后右键此快捷方式,粘贴到下面的桌面中,这样安装完成后自动在桌面上创建快捷方式。......
  • Advanced Installer设置安装最后一步启动软件
    左侧用户界面中选择对话框-ExitDialog在完成操作项中勾选“安装结束时启动应用程序”,在弹出的对话框中选择需要启动的exe文件 ......
  • ContextPath must start with '' and not end with ''
    1、问题运行SpringBoot项目,发生java.lang.IllegalArgumentException异常,原因如下:ContextPathmuststartwith'/'andnotendwith'/'完整的Run输出如下:._________/\\/___'_____(_)______\\\\(()\___......
  • Deep-Learning-Based Spatio-Temporal-Spectral Integrated Fusion of Heterogeneous
    Deep-Learning-BasedSpatio-Temporal-SpectralIntegratedFusionofHeterogeneousRemoteSensingImagesabstract为了解决STF中的生成heterogeneousimages问题:为此,本文首次提出了一种基于新型深度残差循环生成对抗网络(GAN)的异构集成框架。所提出的网络由前向融合部......
  • Spatiotemporal Remote Sensing Image Fusion Using Multiscale Two-Stream Convoluti
    SpatiotemporalRemoteSensingImageFusionUsingMultiscaleTwo-StreamConvolutionalNeuralNetworksabstract地表反射率图像的渐变和突变是现有STF方法的主要挑战。(Gradualandabruptchangesinlandsurfacereflectanceimagesarethemainchallengesinexisting......
  • Function-advanced
    函数进阶1.函数防抖在频率触发的情况下只有等待一定的时间才会触发执行一次代码特点执行之前先清除之前的即将执行操作保证只有最后一次生效优化高频执行JS代码操作提高性能防抖代码实现//使用setTimeout实现函数防抖vart=null;document.onmous......
  • Vue学习笔记之Node Sass version 8.0.0 is incompatible with 4.0.0错误
    输入以下两个命令:npmuninstallnode-sassnpmi-Dsass注:Mac环境如果进行了系统升级,需要重新安装Xcode,执行命令xcode-selectinstall不然会出现如下的错误Mac解决gyp:NoXcodeorCLTversiondetected!报错 如果出现python2的错误gypverb`which`failedE......