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 )
{
Polynomial head = (PtrToNode)malloc(sizeof(struct Node));
head -> Next = NULL;
Polynomial q = head;
while(a || b)
{
Polynomial p =(PtrToNode)malloc(sizeof(struct Node));
p -> Next = NULL;
if(a == NULL || b&&a -> Exponent < b -> Exponent)///防止段错误
{
p -> Exponent = b -> Exponent;
p -> Coefficient = b -> Coefficient;
q -> Next = p;
q = p;
b = b -> Next;
}
else if(b == NULL || a&&a -> Exponent > b -> Exponent)
{
p -> Exponent = a -> Exponent;
p -> Coefficient = a -> Coefficient;
q -> Next = p;
q = p;
a = a -> Next;
}
else
{
if(a -> Coefficient + b -> Coefficient)
{
p -> Exponent = a -> Exponent;
p -> Coefficient = a -> Coefficient + b -> Coefficient;
q -> Next = p;
q = p;
}
a = a -> Next;
b = b -> Next;
}
}
return head;
}

6-3 Add Two Polynomials(20 分)的更多相关文章

  1. pat 1065 A+B and C (64bit)(20 分)(大数, Java)

    1065 A+B and C (64bit)(20 分) Given three integers A, B and C in [−2​63​​,2​63​​], you are supposed t ...

  2. 抛弃EF,20分构建一个属于自己的ORM框架

    Poiuyt_cyc 博客园首页新随笔联系订阅管理随笔 - 11  文章 - 0  评论 - 111 抛弃EF,20分构建一个属于自己的ORM框架 相信EF大家都不陌生了,因为数据库表跟程序实体是一一 ...

  3. PTA 邻接表存储图的广度优先遍历(20 分)

    6-2 邻接表存储图的广度优先遍历(20 分) 试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(V ...

  4. #020PAT 没整明白的题L1-009 N个数求和 (20 分)

    后面的测试点过不去,两个错误一个超时. 目前未解决   L1-009 N个数求和 (20 分)   本题的要求很简单,就是求N个数字的和.麻烦的是,这些数字是以有理数分子/分母的形式给出的,你输出的和 ...

  5. L1-023 输出GPLT (20 分)

    L1-023 输出GPLT (20 分) 给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符.当然,四种字符(不区 ...

  6. PAT 乙级 1074 宇宙无敌加法器 (20 分)

    1074 宇宙无敌加法器 (20 分) 地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为“PAT数”.每 ...

  7. PAT 乙级 1044 火星数字 (20 分)

    1044 火星数字 (20 分) 火星人是以 13 进制计数的: 地球人的 0 被火星人称为 tret. 地球人数字 1 到 12 的火星文分别为:jan, feb, mar, apr, may, j ...

  8. PAT 甲级 1035 Password (20 分)

    1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...

  9. 获取数值型数组中大于60的元素个数,给数值型数组中不足60分的加20分。(数组,for循环,if条件判断语句)

    package com.Summer_0420.cn; /** * @author Summer * 获取数值型数组中大于60的元素个数 * 给数值型数组中不足60分的加20分 */ public c ...

随机推荐

  1. (6)Cocos2d-x 3.0坐标系详解

    Cocos2d-x坐标系和OpenGL坐标系相同,都是起源于笛卡尔坐标系. 笛卡尔坐标系 笛卡尔坐标系中定义右手系原点在左下角,x向右,y向上,z向外,OpenGL坐标系为笛卡尔右手系. 屏幕坐标系和 ...

  2. hdu5184 数论证明

    这题说的给了一个整数n 和一串的括号, 那么要我们计算 最后有n/2对括号的 方案数有多少. 我们可以先预先判断一些不可能组成正确括号对的情况, 然后我们可以将这个问题转化到二维平面上, 令 m = ...

  3. javascript 快速排序方法

    "快速排序"的思想很简单,整个排序过程只需要三步: (1)在数据集之中,选择一个元素作为"基准"(pivot). (2)所有小于"基准"的元 ...

  4. Databases Questions & Answers

    Databases Questions & Answers 1.      What are two methods of retrieving SQL? 2.      What curso ...

  5. win2008R2 bitnami 安装 wamp

    下载官方版本:bitnami-wampstack-5.6.30-1-windows-x64-installer.exe 用管理员权限安装,不然mysql服务可能会安装不上.

  6. 20135320赵瀚青LINUX第四章读书笔记

    概述 什么是进程调度 进程调度:在可运行态进程之间分配有限处理器时间资源的内核子系统. 一.调度策略 4.1进程类型 I/O消耗型进程:大部分时间用来提交I/O请求或是等待I/O请求,经常处于可运行状 ...

  7. HDU 2222 Keywords Search(AC自动机)题解

    题意:给你几个keywords,再给你一段文章,问你keywords出现了几次. 思路:这里就要用到多模匹配算法AC自动机了,AC自动机需要KMP和字典树的知识,匹配时是在字典树上,失配我们就要用到类 ...

  8. 面向对象之php多态

    php是面向对象的脚本语言,而我们都知道,面向对象的语言具有三大特性:封装,继承,多态(接口的多种不同的实现方式即为多态). 封装是类的构建过程,php具有.php也具有继承的特性.唯独这个多态,ph ...

  9. linux突然断电重启,配置文件丢失/程序无法打开/文件损坏

    电脑突然断电,重新开机后发现有的程序无法正常启动,原因是配置文件损坏了.感觉奇怪,为什么在硬盘里的文件会内容丢失? 1.可能:写数据的过程被中断,只完成了一部分.可能会出现乱码(因为只写了几个字节,不 ...

  10. window.frames && iframe 跨页面通信

    1.定义 frames[]是窗口中所有命名的框架组成的数组.这个数组的每个元素都是一个Window对象,对应于窗口中的一个框架. 2.用法 假设iframe 是一个以存在的 iframe 的 ID 和 ...