前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2-1 Add Two Polynomials (20 分)

2-1 Add Two Polynomials (20 分)

作者头像
韩旭051
发布2019-11-08 00:46:46
8500
发布2019-11-08 00:46:46
举报
文章被收录于专栏:刷题笔记刷题笔记

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://cloud.tencent.com/developer/article/1534995

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 functions:

代码语言:javascript
复制
Polynomial Add( Polynomial a, Polynomial b );

where Polynomial is defined as the following:

代码语言:javascript
复制
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:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
4
3 4 -5 2 6 1 -2 0
3
5 20 -7 4 3 1

Sample Output:

代码语言:javascript
复制
5 20 -4 4 -5 2 9 1 -2 0

1.系数为0要跳过

2.上来给你个空表,反手就把另一个返回去

3.记住链表头结点是空的,第二个才存数据

代码语言:javascript
复制
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;//0不存
        }
        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;
    }
    if(a != NULL) before->Next = a;//后面全接上剩下的部分
    else before->Next = b;// 
    return ans;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 2-1 Add Two Polynomials (20 分)
    • Format of functions:
      • Sample program of judge:
        • Sample Input:
          • Sample Output:
          • 1.系数为0要跳过
          • 2.上来给你个空表,反手就把另一个返回去
          • 3.记住链表头结点是空的,第二个才存数据
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档