pat 1002

a1002

题目大意。

两个多项式相加。

解决方法。

构建一个双精度的数组。同时在构建一个索引表。这个索引表来看一共有哪几个是要加入的指数,如果系数为零,那么就把它给加入到索引表里面。然后进行加法运算,如果最后的系数还是为零,那么就减少一个要输出的数值。最后再使用short来进行排序,按倒叙的方法来输入所有的值。

代码

#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() {
    int n,zhishu;
    double xishu;
    double ans[1000];
    fill(ans,ans+1000,0.0);
    int temp[1000];
    int time=0;
    int count=0;
    for (int i = 0; i < 2; ++i) {
        cin>>n;
        for (int j = 0; j <  n; ++j) {
            cin>>zhishu>>xishu;
            if (ans[zhishu]==0.0){
                temp[time]=zhishu;
                time++;
                count++;

            }

            ans[zhishu]+=xishu;
//            如多相加为0
            if (ans[zhishu]==0.0){

                count--;
            }
        }

    }
    cout<<count;
    
    sort(temp,temp+time);
    for (int k = time-1; k >=0 ; --k) {
        if (ans[temp[k]]!=0.0){
            cout<<" "<<temp[k]<<" ";
            printf("%.1lf",ans[temp[k]]);
        }

    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值