多项式求和

 

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

N1​​ aN1​​​​ N2​​ aN2​​​​ ... NK​​ aNK​​​​

where K is the number of nonzero terms in the polynomial, Ni​​ and aNi​​​​ (,) are the exponents and coefficients, respectively. It is given that 1,0.

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 2 1.5 1 2.9 0 3.2
 
 
复制代码
#include <bits/stdc++.h>
using namespace std;
struct Node{
    double ce;
    int  ex;
}n1[100],n2[100],n3[100],n4[100];
bool cmp(Node a,Node b){
    return a.ex>b.ex;
} 
int  vis[100];
int main()
{
        int  k1,k2;
        scanf("%d",&k1);
        for(int  j =0;j<k1;j++){
            scanf("%d%lf",&n1[j].ex,&n1[j].ce);
        }
        scanf("%d",&k2);
        for(int  j =0;j<k2;j++){
            scanf("%d%lf",&n2[j].ex,&n2[j].ce);
        }
        int cnt = 0;
        int  flag  ;
        for(int j=0;j<k1;j++){
            int x=n1[j].ex;
            flag  = 0;
            for(int  k =0;k<k2;k++){
                if(x==n2[k].ex){
                    n3[cnt].ex=x;
                    n3[cnt++].ce=n1[j].ce+n2[k].ce;
                    vis[x]=1;
                    flag  =1;
                    break;
                }
            }
            if(!flag){
                    n3[cnt].ex=x;
                    n3[cnt++].ce=n1[j].ce;
            }    
        }
        for(int i =0;i<k2;i++){
            int x = n2[i].ex;
            if(!vis[x]){
                n3[cnt].ex=x;
                n3[cnt++].ce=n2[i].ce;
            }
        }
        sort(n3,n3+cnt,cmp);
        int cnt1=0;
        for(int i=0;i<cnt;i++){
            if(n3[i].ce!=0){
                n4[cnt1].ex=n3[i].ex;
                n4[cnt1++].ce=n3[i].ce;
        }
        }
        if(cnt1!=0){
        printf("%d ",cnt1);
        for(int i =0;i<cnt1;i++){
            printf("%d %.1f%c",n4[i].ex,n4[i].ce,i==cnt1-1?'\n':' ');
        }
    }
    else{
        printf("0\n");
    }
        return 0;
    
}

    
复制代码

 

posted on   cltt  阅读(532)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2018-09-18 zoj 4057
2018-09-18 zoj 4056
2018-09-18 zoj 4054
2018-09-18 zoj 4049
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示