6-3 Add Two Polynomials (20 分)

Add Two Polynomials 4-3 Add Two Polynomials   (20分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represen 阅读详情

Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node.

Format of functions:

Polynomial Add( Polynomial a, Polynomial b );

where Polynomial is defined as the following:

typedef struct Node *PtrToNode;
struct Node {
    int Coefficient;
    int Exponent;
    PtrToNode Next;
};

typedef PtrToNode Polynomial;
/* Nodes are sorted in decreasing order of exponents.*/  

The function Add is supposed to return a polynomial which is the sum of a and b.

Sample program of judge:

#include <stdio.h>
#include <stdlib.h>
typedef struct Node *PtrToNode;
struct Node  {
    int Coefficient;
    int Exponent;
    PtrToNode Next;
};
typedef PtrToNode Polynomial;

Polynomial Read(); /* details omitted */
void Print( Polynomial p ); /* details omitted */
Polynomial Add( Polynomial a, Polynomial b );

int main()
{
    Polynomial a, b, s;
    a = Read();
    b = Read();
    s = Add(a, b);
    Print(s);
    return 0;
}

/* Your function will be put here */

Sample Input:

4
3 4 -5 2 6 1 -2 0
3
5 20 -7 4 3 1

Sample Output:

5 20 -4 4 -5 2 9 1 -2 0

题意:给两个由单链表组成的多项式,其中指数由大到小排列,将这两个式子相加,合并成一个多项式。注意:输入的两个链表是带头结点的, 合并后也要带头结点。另外,系数相加等于零的项需要消去。

代码:

Polynomial Add( Polynomial a, Polynomial b ){
    if(a == NULL) return b;
    if(b == NULL) return a;
    Polynomial ans, tmp, before; //ans是头结点, before指向tmp前一个结点
    before = (Polynomial)malloc(sizeof(Polynomial));
    ans = before;
    a = a->Next;
    b = b->Next;
    while(a != NULL && b != NULL){
        tmp = (Polynomial)malloc(sizeof(Polynomial));
        before->Next = tmp;
        if(a->Exponent == b->Exponent){
            tmp->Exponent = a->Exponent;
            tmp->Coefficient = a->Coefficient + b->Coefficient;
            a = a->Next;
            b = b->Next;
            if(tmp->Coefficient == 0)
                continue;
        }
        else if(a->Exponent > b->Exponent){
            tmp->Exponent = a->Exponent;
            tmp->Coefficient = a->Coefficient;
            a = a->Next;
        }
        else{
            tmp->Exponent = b->Exponent;
            tmp->Coefficient = b->Coefficient;
            b = b->Next;
        }
        before = tmp;
        tmp = tmp->Next;
    }
    if(a != NULL) before->Next = a;
    else before->Next = b;

    return ans;
}
2-1 Add Two Polynomials (20分) 2-1 Add Two Polynomials (20分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node. Format of fun 阅读详情

相关推荐

c 编写Add Two Polynomials

题目:  Add Two Polynomials    Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an

vason的博客 1627

2-1 Add Two Polynomials(多项式相加)(附测试样例)

编写一个函数来实现两个多项式相加。不要破坏输入。使用带有头节点的链表实现。注:零多项式由一个空列表表示,其中只有头节点。

JYHZZ的博客 1918

数据结构作业系统第二章答案

数据结构的作业系统答案。。第二章的。。分两个文件。。可以复制到作业系统就可以运行了

数据结构基础之Add Two Polynomials

6-1 Add Two Polynomials (8 分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node. Format of fun

LuvLucifer的博客 1202

c语言 单链表 两个多项式 相加。

用单链表实现两个多项式的相加。压缩文件里是一个工程。可用VC直接打开工程。

第8.1节 满足构网要求的功率器件与散热设计

摘要: 构网型变流器的功率器件选型与散热设计面临严苛要求,需综合考虑过载能力、暂态应力与可靠性。IGBT模块选型需重点评估电压/电流等级、损耗特性及结温波动,而SiC MOSFET虽具有高频低损耗优势,但面临栅极可靠性等挑战。散热设计需采用液冷等先进技术控制结温波动,并通过电热应力仿真和寿命预测实现可靠性优化。系统化选型流程需结合规范要求、工况谱分析和实测验证,为构网型变流器提供坚实的物理基础。

技术工作者 915

6-3-Add Two Polynomials-函数题

6-3-Add Two Polynomials-函数题解题代码测试结果问题整理 解题代码 Polynomial Add(Polynomial a, Polynomial b) { Polynomial add = (Polynomial)malloc(sizeof(struct Node)); Polynomial ret = add; add->Next = NULL; Polyn...

Aruasg的博客 783

集成电路模拟版图入门-版图基础学习笔记(四)

第三部分:版图的准备 2. 设计规则 3) Design Rule 2. 设计规则 4) 规则定义 4.1 NW(N WELL) 2. 设计规则 4) 规则定义 4.1 NW(N WELL) 2. 设计规则 4) 规则定义 4.2 PO(Poly) 2. 设计规则 4) 规则定义 4.3 M1(Metal1) 2. 设计规则 4) 规则定义 4.4 VIA 3. DRC文件 3.1 DRC:Design Rule Check,设计规则检查。 3.2 DRC程序了解有关

weixin_52955333的博客 6737

2-1 Add Two Polynomials

Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dumm...

weixin_43728310的博客 292

你不必懂AI,但必须会用AI:写给所有焦虑中的职场人

AI时代不必焦虑学技术,关键在于用AI解决实际问题。文章指出,99%的人只需成为AI使用者而非建造者,核心竞争力在于结合业务痛点运用AI工具提升效率。与其盲目学习工具,不如梳理工作流程,识别可优化环节,让AI赋能重复性、标准化任务。行业经验与AI工具的结合才是真正的职场护城河。建议从具体工作场景出发,寻找匹配的AI解决方案,迈出应用第一步。

hundred98的博客 718

热膨胀系数(CTE)的不同处理方法对于封装翘曲仿真的影响

随着技术的不断发展,芯片封装的集成度越来越高,尺寸也越来越大,随之而来的封装翘曲问题也越来越引起关注。影响芯片封装翘曲最主要的原因就是封装中不同材料热膨胀系数(CTE)的失配性,因此在进行封装翘曲仿真的过程中,如何使用材料的CTE值就变得尤为重要,会对仿真精度产生较大的影响。本文将对比不同CTE的处理方法给仿真结果带来的影响。

wangyubo1的博客 6857

2-1 Add Two Polynomials(20分)

注意等于零消项

Zhonexixi的博客 149

【AI智能体】Skills 智能体驱动开发从使用到项目实战详解

Skills 智能体驱动开发从使用到项目实战详解

congge 6612

6-3 Add Two Polynomials(20 分)

6-3 Add Two Polynomials(20 分)Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an ...

友人A 1925

【华为OD机试真题 2025B卷】737、统计监控器、需要打开多少监控器 | 机试真题+思路参考+代码分析(C++、Java、Py、C语言、JS)

【华为OD机试真题 2025B卷】737、统计监控器、需要打开多少监控器 | 机试真题+思路参考+代码分析(C++、Java、Py、C语言、JS) 华为od机试,华为od机试真题,华为od机试E卷,华为odE卷,华为od机试真题E卷,华为OD,华为OD机试,华为OD机试真题,华为od机考,华为od机试D卷,华为od机试真题D卷,华为od手撕代码,华为od,华为od机试C卷,华为od e卷,华为2025e卷,od机试,2025E卷,华为OD机试E卷,什么是华为od,华为od介绍,全面了解华为od,2025华为

KJ.JK 716

基于卷积神经网络CNN实现电力负荷多变量时序预测(PyTorch版)

随着智能电网的发展,精准的电力负荷预测对能源调度和电网稳定具有重要意义。本文基于PyTorch框架,提出一种基于卷积神经网络(CNN)的多变量时间序列预测模型,用于电力负荷的短期预测。

时序笔记 988

6-3 Add Two Polynomials (20分)(附测试点)

Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node. Format of functions: Polynomial Add( Polyno

xxxibgdrgn博客 3386

数字盲水印嵌入和提取的实现基于小波变换DWT算法

数字盲水印是指在不知道原始图像的情况下,在其上嵌入水印并在后续的提取过程中能够恢复出原始水印的一种技术。本文介绍的数字盲水印嵌入和提取的实现方法基于小波变换DWT算法。具体来讲,DWT将信号或图像进行二次下采样和卷积滤波,使得高频分量的图像信息被集中在一个小区域内,方便进行嵌入和提取操作。其中 a 表示嵌入强度,而 watermark 则是用于嵌入的水印图像,这里我们嵌入的是一个全灰的图像。通过以上的代码,我们可以实现数字盲水印的嵌入和提取,并在一定程度上保护数字图片的版权安全。

HackWhisper的博客 513

2-1 Add Two Polynomials (20 分)

2-1Add Two Polynomials(20分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by ...

韩旭051的博客 1991

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

【11】Add Two Polynomials

2-1 Add Two Polynomials (20分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node.

WR的博客 1288

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

数据结构练习题 2-1 Add Two Polynomials (20分)

2-1 Add Two Polynomials (20分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node. Format of fun

rookielsj的博客 2125

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

上一篇: [PAT甲级] 1018 Public Bike Management (DFS)
Sternley
博客等级 码龄9年 0粉丝 7原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值