当前位置:网站首页>6-43 sum of ordered sparse polynomials
6-43 sum of ordered sparse polynomials
2022-06-22 09:35:00 【Xia ~ Chen】
This problem is to find the sum of two ordered polynomials , The input polynomial has two terms : Coefficients and exponents .
Polynomials are stored in chains , And arrange them according to the index from small to large .
The output is also in the order of exponent from small to large .
( Input data in descending order of index , Input 0 0 The hour means the end . The construction part has been completed , No need to write it yourself )
To simplify the construction of linked list , The default construction linked list is the supervised single linked list .
Function interface definition :
ptr add(ptr ha,ptr hb);add Are two ordered linked lists ha,hb Do the addition , Form a new linked list and return , The original list ha,hb Retain .
Sample referee test procedure :
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#define N 5
typedef struct node
{
float ceof;
int exp;
struct node *next;
}node,*ptr;
ptr creat();// The tail of the table is inserted to construct the linked list , It's not here
ptr add(ptr ha,ptr hb);
void output(ptr h)
{
ptr p;
p=h->next;
while(p!=NULL)
{
printf("%+.1fx^%d",p->ceof,p->exp);
p=p->next;
}
printf("\n");
}
int main()
{
ptr h1,h2,h3;
h1=creat();
h2=creat();
h3=add(h1,h2);
output(h1);
output(h2);
output(h3);
return 0;
}
/* Please fill in the answer here */sample input :
-19 0
21 2
90.2 5
100 10
18 92
0 0
19 0
-21 2
65 4
-8 5
43.6 14
0 0No blank lines at the end
sample output :
-19.0x^0+21.0x^2+90.2x^5+100.0x^10+18.0x^92
+19.0x^0-21.0x^2+65.0x^4-8.0x^5+43.6x^14
+65.0x^4+82.2x^5+100.0x^10+43.6x^14+18.0x^92Ideas : According to the title, we need to add and subtract the two ordered linked lists , The construction of the linked list has been completed for us ( The damn question hides this part of the code , You can contact me if you need me ), First, keep the original linked list , We definitely need to create a new linked list to store the results of adding coefficient polynomials , When you see a friend here, you may ask , How to ensure order ? Um. .... The title has been made for you , Never mind , Just compare and add directly , I don't say much nonsense , Code up .
Code up :
ptr add(ptr ha, ptr hb)
{
ptr h, s, p;
h = s=(ptr)malloc(sizeof(node));// Create a new linked list to store the results of the addition of the two linked lists
ha=ha->next;// Leading one-way linked list , Remember that the assignment of the tail node is null
hb=hb->next;
while (ha&&hb)// This is to calculate the same length of two linked lists , Some watches may be long , Some may be short , This time in short
// Compare based on the length of the linked list , The long part , Just bring it here
{
if (ha->exp < hb->exp) // Compare index sizes
{
p = (ptr)malloc(sizeof(node));
p=ha;
ha = ha->next;
s->next = p;
s = p;
}
else if(ha->exp==hb->exp)
{
p = (ptr)malloc(sizeof(node));
if(ha->ceof+hb->ceof==0)
{
ha=ha->next;
hb=hb->next;
}
else
{
p->exp=ha->exp;
p->ceof=ha->ceof+hb->ceof;
ha=ha->next;
hb=hb->next;
s->next=p;
s=p;
}
}
else
{
p = (ptr)malloc(sizeof(node));
p=hb;
hb = hb->next;
s->next = p;
s = p;
}
}
while (ha != NULL)// Operate on the long part
{
p = (ptr)malloc(sizeof(node));
p=ha;
ha = ha->next;
s->next = p;
s = p;
}
while (hb != NULL)
{
p = (ptr)malloc(sizeof(node));
p=hb;
hb = hb->next;
s->next = p;
s = p;
}
s->next =NULL;// The value of the tail node is set to null
return h;// Return the head node of the new linked list , I still have to output
}
边栏推荐
- Overview of microservice architecture
- PAT甲级 1016 Phone Bills(时间差)
- [cmake命令笔记]find_path
- Function summary (1)
- Solutions to prompt non standard import in go language
- Why use gradient descent method
- SQL编程task04作业-集合运算
- Stream stream creation_ Operation_ Collection_ case
- Two threads execute i++ 100 times respectively, and the possible values obtained
- 秋招秘籍A
猜你喜欢

Langchao state-owned assets cloud: state-owned assets as a guide to help state-owned enterprises use data to enrich their wisdom in the cloud

在ensp上做防火墙的双机热备

为啥要使用梯度下降法

Project optimization + online (Master?)

逻辑回归和线性回归
![[学习笔记] 回滚莫队](/img/19/d374dd172b9609a3f57de50791b19e.png)
[学习笔记] 回滚莫队

Shengdun technology joined dragon lizard community to build a new open source ecosystem
![Let you get started [uni app]](/img/e7/bc33d54a345901f67afa856769045f.png)
Let you get started [uni app]

实现AD环境下多用户隔离FTP
![[node] theory + practice enables you to win sessions and cookies](/img/26/ef15896094d1887619c6ba9f6f287a.png)
[node] theory + practice enables you to win sessions and cookies
随机推荐
==经典面试题
Find the size of cosine
5道面试题,拿捏String底层原理!
[Luogu] P1083 [NOIP2012 提高组] 借教室(线段树)
Function summary (1)
Network security Kali penetration learning how to conduct Nessus vulnerability detection
Byte/byte?别搞晕了!
Cocoscreator compilation error record could not write cache value gradle\daemon\4.10.3\regi
一个老开源人的自述-如何干好开源这件事
招募令|数据可视化开发平台“FlyFish”「超级体验官」招募啦
Detr: end to end object detection with transformers (from Li Mu and Zhu)
Feedforward and backpropagation
IS_ ERR()
Centos7安装PostgreSQL12
循环队列超详细实现,小白秒懂
How C processes use non static methods
在ensp上做防火墙的双机热备
论文笔记:DETR: End-to-End Object Detection with Transformers (from 李沐老师and朱老师)
6-44 traversal of binary tree
[hdu] P1466 计算直线的交点数