1009 Product of Polynomials (25 分)

题目要求:多项式相乘,总体思路是双层循环,前一个多项式的每一项都和第二个多项式的n项相乘,根据指数,累加其系数

总体难度不大,但有几个细节:系数和指数可以用map<int, double>存储,默认值为0

如果计算某一项发现系数为0了,直接删除该指数的map即可

最后需要倒序输出,因为map会对键从小到大排序,和题目相反

map倒序输出可以使用reverse_iterator

#include<cstdio>
#include<iostream>
#include<map>
using namespace std;


int main(){
    map<int,double> a;
    map<int,double> b;
    map<int,double> c;
    int n, m;
    scanf("%d",&n);
    
    for(int i=0;i<n;i++){
        int ex;
        double coe;
        scanf("%d%lf",&ex,&coe);
        a[ex] = coe;
        
    }
    
    scanf("%d",&m);
    for(int i=0;i<m;i++){
        int ex;
        double coe;
        scanf("%d%lf",&ex,&coe);
        b[ex] = coe;
    }
    
    for(map<int,double>:: iterator it1 = a.begin();it1 != a.end();it1++){
        int sum = 0;
        for(map<int,double>:: iterator it2 = b.begin();it2 != b.end();it2++){
            
            
            c[it1->first+it2->first] += it1 -> second * it2 -> second;
            if(c[it1->first+it2->first] == 0){
                c.erase(it1->first+it2->first);
            }
        }
    }
    printf("%d ",c.size());
    for(map<int,double>:: reverse_iterator it = c.rbegin();it != c.rend();){
        
        printf("%d %.1lf",it->first,it->second);
        if(++it != c.rend()) printf(" ");
        else printf("\n");
    }
    //printf("%lf",arr[1]);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值