首页 > 其他 > 详细

PAT甲级 1002 A+B for Polynomials

时间:2021-02-15 23:32:06      阅读:28      评论:0      收藏:0      [点我收藏+]

 

读懂题意非常重要

Polynomials:多项式

exponents and coefficients:指数和系数

题意:给你每个x的指数和当前x的指数的系数,求两个多项式相加。

题意了解明白是不是so easy

然而还有坑点:1.保留一位小数,2.相加为0的不输出,3.所有式子都为0,只输出0.

#include<bits/stdc++.h>
using namespace std;
int k,n,sum,cnt=1;
double a;
double ans[1010];
map<int,double,greater<int> > ma;
int main(){
    cin>>k;
    for(int i=0;i<k;i++){
        cin>>n>>a;
        ma[n]=a;
    }
    cin>>k;
    
    for(int i=0;i<k;i++){
        cin>>n>>a;
        ma[n]=ma[n]+a;
    }

    for (map<int, double>::iterator it = ma.begin(); it != ma.end(); it++) {
        if(it->second ==0) continue;
        else{
            ans[cnt++]=it->first;
            ans[cnt++]=it->second;
        }
    }
    if(cnt==1) {
        cout<<0;
        return 0;
    } 
    cout<<cnt/2;
    for(int i=1;i<cnt;i+=2){
        printf(" %.0lf %.1lf",ans[i],ans[i+1]);
    }
    return 0;
} 

 

PAT甲级 1002 A+B for Polynomials

原文:https://www.cnblogs.com/lyj1/p/14404354.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!