pat 1009

a1009

题目大意

多项式乘法。

解决方法

与前面的多项式加法基本类似。不过要构建两个双精度数组。第一个保存第一列的数字系数和指数。然后再还有一个哈希表。这个是保存第一列由哪几个数字是要进行指数相加的。第二列就首先是遍历这个哈希表。然后进行赋值。如果bi不是零,那么次数就加一。经过加法后,如果bi的值为0,那么次数就减一。,然后再次利用sort函数进行倒序输出就可以了。

代码

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>

//cin用多了超市

using namespace std;
const int maxn=10010;

int main() {
    double  a[maxn],b[maxn];
    fill(a,a+maxn,0.0);
    fill(b,b+maxn,0.0);
    int temp[maxn];
    int count=0;
//    统计一共有多少个
    int a_index=0;
    int hash[maxn];
    int total=0;
    for (int i = 0; i < 2; ++i) {
        int n,zhishu;
        double xishu;
        cin>>n;
        for (int j = 0; j < n; ++j) {
            cin>>zhishu>>xishu;
            if (i==0){
                a[zhishu]=xishu;
                temp[a_index++]=zhishu;
            } else{
                for (int k = 0; k < a_index; ++k) {

                    int weizhi=zhishu+temp[k];
                    if (b[weizhi]==0.0){
                        hash[count]=weizhi;
                        count++;
//                        系数增加
                        total++;
                    }
                    b[weizhi]+=xishu*a[temp[k]];
                    if (b[weizhi]==0.0){
                        total--;
                    }
                }
            }
        }
    }
    cout<<total;
    sort(hash,hash+count);

    for (int l = count-1; l >=0 ; --l) {
        if (b[hash[l]]!=0.0)
        printf(" %d %0.1lf",hash[l],b[hash[l]]);

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值