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( 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 分)的更多相关文章
- pat 1065 A+B and C (64bit)(20 分)(大数, Java)
1065 A+B and C (64bit)(20 分) Given three integers A, B and C in [−263,263], you are supposed t ...
- 抛弃EF,20分构建一个属于自己的ORM框架
Poiuyt_cyc 博客园首页新随笔联系订阅管理随笔 - 11 文章 - 0 评论 - 111 抛弃EF,20分构建一个属于自己的ORM框架 相信EF大家都不陌生了,因为数据库表跟程序实体是一一 ...
- PTA 邻接表存储图的广度优先遍历(20 分)
6-2 邻接表存储图的广度优先遍历(20 分) 试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(V ...
- #020PAT 没整明白的题L1-009 N个数求和 (20 分)
后面的测试点过不去,两个错误一个超时. 目前未解决 L1-009 N个数求和 (20 分) 本题的要求很简单,就是求N个数字的和.麻烦的是,这些数字是以有理数分子/分母的形式给出的,你输出的和 ...
- L1-023 输出GPLT (20 分)
L1-023 输出GPLT (20 分) 给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符.当然,四种字符(不区 ...
- PAT 乙级 1074 宇宙无敌加法器 (20 分)
1074 宇宙无敌加法器 (20 分) 地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为“PAT数”.每 ...
- PAT 乙级 1044 火星数字 (20 分)
1044 火星数字 (20 分) 火星人是以 13 进制计数的: 地球人的 0 被火星人称为 tret. 地球人数字 1 到 12 的火星文分别为:jan, feb, mar, apr, may, j ...
- PAT 甲级 1035 Password (20 分)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...
- 获取数值型数组中大于60的元素个数,给数值型数组中不足60分的加20分。(数组,for循环,if条件判断语句)
package com.Summer_0420.cn; /** * @author Summer * 获取数值型数组中大于60的元素个数 * 给数值型数组中不足60分的加20分 */ public c ...
随机推荐
- IBM WebSphere MQ for net 报错 MQRC_NOT_AUTHORIZED
最近进入新公司要维护以前的90年代的老系统 用NET对IBMMQ做测试 NET 4.0 +7.5 MQ 版本 待我写好NET调用的代码后出现错误MQRC_NOT_AUTHORIZED 折腾大半天往上找 ...
- 表单验证—html5新特性表单验证
一.表单 <body> <section id="register"> <div><img src="images/logo.j ...
- VS2010/MFC编程入门之六(对话框:创建对话框模板和修改对话框属性)
鸡啄米在上一讲中介绍了MFC的消息映射机制,属于原理方面的知识.对于VC++编程入门学习者来说可能有些抽象,鸡啄米会把消息映射的知识渗透到后面的教程中.本节开始为大家讲解偏应用的知识-创建对话框. 对 ...
- Codeforces Round #528 Solution
A. Right-Left Cipher Solved. 注意长度的奇偶 #include <bits/stdc++.h> using namespace std; string s; i ...
- 计算机bit是什么意思
bit是计算机中数据的最小单位,即二进制位,数字0和1 一个字节是八位(8个0和1 或 1 组成的一串二进制) 一个字是16位,等于2个字节 用八位二进制表示的字符叫单字节字符, 用16位二进制数表示 ...
- Python:数字的格式化输出
>>> 'The value is {:0,.2f}'.format(x) 'The value is 1,234.57' 需要将数字格式化后输出,并控制数字的位数.对齐.千位分隔符 ...
- 一个简单清晰的Redis操作类-php
<?php /** * redis处理的二次封装 * */ class Redis{ private $_redis; private $_config; public function __c ...
- URL存在http host头攻击漏洞-修复方案
URL存在http host头攻击漏洞-修复方案 spring boot使用注解的方式 -- 第一步:在自定义filter类上添加如下注释 package com.cmcc.hy.mobile.con ...
- Qt5.4.1_静态编译
http://www.cnblogs.com/findumars/p/4852350.html http://godebug.org/index.php/archives/133/ http://ww ...
- JAVA异常处理机制分析(上)
过去曾有一段时间关于java的异常处理机制曾经让我吃尽苦头,异常机制看似简单,原理,用法也仅仅如此,但是,用起来或是在使用一些框架的时候总会因为使用不当,造成灾难性后果. jdk异常处理机制 ...