PAT A1009 Product of Polynomials

在这里插入图片描述
exponent 是int型那个,每个数字可化为对应字母权值,相乘后权值相等的要加在一起
coefficient 系数直接两个vector挨个相乘好了

两个vector保存两行输入
vres 进行计算,这里相同的exponent在a[]数组中进行合并
a的i就是最终合并完的exponent,a[i]就是coefficient
放到vfinal中去,因为样例的输出是从exponent从大到小,所以放入vfinal时要从2005(K<1000)开始往0处理

做的时候第一个样例应该是类似于
1 0 2.0
1 0 2.0
输出应该是1 0 4.0
漏了处理a[0]for语句中补上

4ms,400KB左右

#include<bits/stdc++.h>
using namespace std;
struct Node{
    double coefficient;
    int exponent;
}node;
double a[2005] = {0};
int main()
{
    int K1;
    scanf("%d", &K1);
    vector<Node> v1, v2, vres;
    for(int i = 0; i < K1; i++){
        cin >> node.exponent >> node.coefficient;
        v1.push_back(node);
    }
    int K2;
    scanf("%d", &K2);
    for(int i = 0; i < K2; i++){
        cin >> node.exponent >> node.coefficient;
        v2.push_back(node);
    }
    for(int i = 0; i < K1; i++){
        for(int j = 0; j < K2; j++){
            node.coefficient = v1[i].coefficient * v2[j].coefficient;
            node.exponent = v1[i].exponent + v2[j].exponent;
            vres.push_back(node);
        }
    }
    for(int i = 0; i < vres.size(); i++){
        a[vres[i].exponent] += vres[i].coefficient;
    }
    vector<Node> vfinal;
    for(int i = 2005; i >= 0; i--){
        if(a[i]!=0){
            node.coefficient = a[i];
            node.exponent = i;
            vfinal.push_back(node);
        }
    }
    cout << vfinal.size() << " ";
    for(int i = 0; i < vfinal.size()-1; i++){
        printf("%d %.1f ", vfinal[i].exponent, vfinal[i].coefficient);
    }
    printf("%d %.1f", vfinal[vfinal.size()-1].exponent, vfinal[vfinal.size()-1].coefficient);
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值