首页 > 其他分享 >PAT Advanced 1009. Product of Polynomials

PAT Advanced 1009. Product of Polynomials

时间:2023-05-10 21:25:42浏览次数:35  
标签:tempCoef Product tempExp num1 res second 1009 PAT termCount

PAT Advanced 1009. Product of 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 product 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 up 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 3 3.6 2 6.0 1 1.6

6. Performance Limit:

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

思路:

利用map<int, double>类型存储多项式 \(A\) 和 \(B\),以及二者相乘的结果res。模拟多项式乘法,遍历 \(A\) 的每一项与 \(B\) 的每一项相乘并存储结果到res,最后遍历res统计非零项的个数并进行输出。

My Code & Result:

#include <iostream>
#include <map>

using namespace std;

int main(void)
{
    map<int, double> num1, num2;
    map<int, double> res;
    int termCount=0;
    int i=0; // iterator
    int tempExp = 0;
    double tempCoef = 0.0;

    cin >> termCount;
    for(i=0; i<termCount; ++i) // input num1
    {
        cin >> tempExp >> tempCoef;
        num1[tempExp] = tempCoef;
    }

    cin >> termCount;
    for(i=0; i<termCount; ++i) // input num2
    {
        cin >> tempExp >> tempCoef;
        num2[tempExp] = tempCoef;
    }

    for(const auto &i : num1) // range for num1
    {
        for(const auto &j : num2) // range for num2
        {
            tempExp = i.first + j.first;
            tempCoef = i.second * j.second;
            res[tempExp] += tempCoef;
        }
    }

    termCount=0;
    for(const auto & w: res) // count the nonzero term
    {
        if(w.second != 0.0) ++termCount;
    }

    cout << termCount;

    for(auto it= res.rbegin(); it != res.rend(); ++it)
    {
        if(it->second != 0.0)
        {
            printf(" %d %.1f", it->first, it->second);
        }
    }
    printf("\n");
    
    // for(const auto &w:num1) // range for num1
    // {
    //     cout << w.first << " " << w.second << " ";
    // }
    // cout << endl;
    // for(const auto &w:num2) // range for num2
    // {
    //     cout << w.first << " " << w.second << " ";
    // }
    
    return 0;
}
Compiler
C++ (g++)
Memory
468 / 65536 KB
Time
4 / 400 ms

标签:tempCoef,Product,tempExp,num1,res,second,1009,PAT,termCount
From: https://www.cnblogs.com/tacticKing/p/17389355.html

相关文章

  • homebrew 安装报错 Warning: /opt/homebrew/bin is not in your PATH.
    如下报错解决方案编辑 zshrcvim~/.zshrc配置如下  exportPATH="/opt/homebrew/bin:$PATH"  ......
  • Vue的Router 在首页获取 fullPath 一直都是根路由‘/‘ ?
    在main.j中获取的this.$route.fullpath一直都是'/',因为给路由fullPath赋值是微任务,我们直接获取肯定只能拿到根路由“/”;解决方案:1.给路由fullPath赋值是微任务,那么只需要通过宏任务获取fullPath就可以了,setTimeout(()=>{console.log(this.$route.fullPath)},2000) 2......
  • Linux 设置 LD_LIBRARY_PATH
    转载:https://www.cnblogs.com/zhanggaofeng/p/7535034.html 在Linux下,如果自己写好一个动态链接库,需要在其他程序里调用,则需要让这些程序能找到这个动态链接库,如果设置的不对,就会出现类似的错误:errorwhileloadingsharedlibraries:libmysqlclientso.so.0:cannotopens......
  • 建造者模式(Builder Pattern)
    模式动机建造者模式(BuilderPattern)是最复杂的创建型模式,它用于创建一个包含多个组成部分的复杂对象,可以返回一个完整的产品对象给用户。它通过将客户端与包含多个组成部分的复杂对象的创建过程分离,使得客户端无需知道复杂对象的内部组成部分与装配方式,只需要知道建造者的类型......
  • 1009 说反话(C++)
    一、问题描述:给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有......
  • 俄大神 lopatkin Windows 精简优化系统 - 工具软件
          昨天有个网友邮件我,说是想找个Tiny7Rev2的ISO操作系统文件,但是我找了下,以前的那些文件有些已经删除了,所以就在网上搜到了俄大神lopatkinWindows精简优化系统,特此放到网盘上让大家能够下载。      链接:https://pan.baidu.com/s/1w6rsaLhNGI35tT6WX46ijA......
  • pathon
    #第一步,建立字典dic_menu:dic_menu={"蔬菜":{"青菜":"绿色","胡萝卜":"橙色","茄子":"紫色","毛豆":"绿色"},"水果":{"山竹":"紫色","香蕉":"黄色",&q......
  • 单例模式(Singleton Pattern)
    单例模式模式动机单例模式(SingletonPattern)是结构最简单的设计模式,它的核心结构中只包含一个被称为单例类的特殊类。通过单例模式可以确保系统中一个类只有一个实例,且该实例易于被外界访问,从而方便对实例个数的控制并节约系统资源。如何确保一个类只有一个实例并且这个实......
  • (转)IBM Product -> Description 表
    IBMProduct->Description表产品型号描述硬盘364673GB15KRPMSASDiskDrive3647146GB15KRPMSASDiskDrive3648300GB15KRPMSASDiskDrive3649450GB15KRPM,3.5inch,SASDiskDrive196873.4GB10,000RPMUltra320SCSIDiskDriveAssembly196914......
  • 更新macOS系统后,使用gcc/g++命令,提示错误xcrun: error: invalid active developer pat
      更新macOS系统后,使用gcc/g++命令编译程序,提示错误xcrun:error:invalidactivedeveloperpath(/Library/Developer/CommandLineTools),missingxcrunat:/Library/Developer/CommandLineTools/usr/bin/xcrun解决方法:重新安装CommandLineTools,一般安装完成后问题就能......