c语言多项式相加

这是源程序大侠们看看哪里有问题我就是调试部出来#include<stdio.h>#include<stdlib.h>#include<conio.h>#include<m... 这是源程序 大侠们看看哪里有问题 我就是调试部出来
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
#include<malloc.h>

typedef struct polynode
{
float coef;
int exp;
struct polynode *next;
}NODE;

printpoly(NODE*);
NODE *createpoly(int);
NODE *polyadd(NODE*,NODE*);

int main(void)
{
NODE *poly1,*poly2,*poly3;
int H=0;
printf("\n === 多项式相加 === \n");
printf("\n 1.第一个多项式poly1(x)共有多少项:");
scanf("%d",&H);
printf("\n");
poly1=createpoly(H);
printf("\n");
printf("\n 2.第二个多项式poly2(x)共有多少项:");
scanf("%d",&H);
poly2=createpoly(H);
poly3=polyadd(poly1,poly2);
printf("\n 1. poly1(x)=");
printpoly(poly1);
printf("\n 2. poly2(x)=");
printpoly(poly2);
printf("\n 3. poly3(x)=");
printpoly(poly3);
printf("\n");
return 0;
}

printpoly(NODE *ptr)
{
char opr;
while(ptr!=NULL)
{
if(ptr->coef>0)
{
opr='+';
}
else
{
opr='-';
}
printf("%c%3.1fx^%d",opr,fabs(ptr->coef),ptr->exp);
}
}

NODE *createpoly(int n)
{
float coef=0;
int exp=0;
int i=0;
NODE *head,*tail,*ptr;
head=tail=NULL;
do{
i++;
ptr=malloc(sizeof(NODE));
printf(" 输入第(%d)项的系数和指数(用空格分开): ",i);
scanf("%f %d",&coef,&exp);
ptr->coef = coef;
ptr->exp = exp;
ptr->next=NULL;
if(head=NULL)
head=ptr;
else
tail=ptr;
}while(n>i);
return head;
}

NODE *polyadd(NODE *poly1,NODE *poly2)
{
NODE *tail,*head, *ptr1,*ptr2,*ptr3;
ptr1=poly1;
ptr2=poly2;
head=tail=NULL;
while(ptr1!=NULL && ptr2!=NULL)
{
ptr3=malloc(sizeof(NODE));
if(ptr1->exp>ptr2->exp)
{
ptr3->coef=ptr1->coef;
ptr3->exp=ptr1->exp;
ptr1=ptr1->next;
}
else if(ptr1->exp<ptr2->exp)
{
ptr3->coef=ptr2->coef;
ptr3->exp=ptr2->exp;
ptr2=ptr2->next;
}
else
{
ptr3->coef=ptr1->coef+ptr2->coef;
ptr3->exp=ptr1->exp;
ptr1=ptr1->next;
ptr2=ptr2->next;
}
ptr3->next=NULL;
if(head==NULL)
head=ptr3;
else
tail->next=ptr3;
tail=ptr3;
}
if(ptr1==NULL)
{
while(ptr2!=NULL)
{
ptr3=malloc(sizeof(NODE));
ptr3->coef=ptr2->coef;
ptr3->exp=ptr2->exp;
ptr2=ptr2->next;
tail->next=ptr3;
tail=ptr3;
}
}
else
{
while(ptr1!=NULL)
{
ptr3=malloc(sizeof(NODE));
ptr3->coef=ptr1->coef;
ptr3->exp=ptr1->exp;
ptr1=ptr1->next;
tail->next=ptr3;
tail=ptr3;
}
}
return head;
}
展开
 我来答
百度网友0974edce8
2009-11-12 · TA获得超过490个赞
知道小有建树答主
回答量:381
采纳率:0%
帮助的人:252万
展开全部
我这有一个实现加减乘除的多项式程序,自己写的,另外输入形式为:-2x^3 +5x^2+3x+4 即可。

其中百度的现实问题,有一个©A和©B的 应该是& COPYA, & COPYB
去掉中间空格

支持整数多项式加减乘除。
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

typedef struct _POLYNODE{
int coef;//系数
int exp;//指数
struct _POLYNODE *next;
}polynode,*polyptr;

void createPoly(polynode **P, char ch[]);//建立多项式链表
void polyAdd(polynode *A,polynode *B);//多项式加
void polyMinus(polynode *A,polynode *B);//减
void polyMulti(polynode *A,polynode *B);//乘
void polyDiv(polynode *A,polynode *B);//除
void order(polynode **P);//排序
void display(polynode *P);//展示多项式
void destroy(polynode **P);//销毁多项式
void menu();//命令菜单
int isPut(char ch[]);
//菜单
void menu(){
printf("1.输入多项式.\n"
"2.多项式相加.\n"
"3.多项式相减.\n"
"4.多项式相乘.\n"
"5.多项式相除.\n"
"6.显示多项式.\n"
"7.销毁多项式.\n"
"8.退出.\n");
}
//判断菜单选择
int IsChoice(int choice){
if(0 < choice && 9 > choice)
return 1;
else
return 0;
}

int isPut(char ch[]){
int i,j = 1;
for(i = 0; ch[i] != '\0'; i++){
{if(0 == j && '^' == ch[i])
return 0;
if('^' == ch[i] && 1 == j)
j = 0;
if(('+' ==ch[i] || '-' == ch[i] || '*' == ch[i] || '/' == ch[i]) && 0 == j)
j = 1;

}
if('.' != ch[i] && 'x' != ch[i] && 'X' != ch[i] && '^' != ch[i] && '+' != ch[i] && '-' != ch[i] && '*' != ch[i] && '/' != ch[i] && !isdigit(ch[i]))
return 0;
else{
if('+' ==ch[0] || '*' == ch[0] || '/' == ch[0] || '^' == ch[0] || '.' == ch[0])
return 0;
if('\0' == ch[i+1] && '+' ==ch[0] || '*' == ch[0] || '/' == ch[0] || '^' == ch[0])
return 0;
// 上面是判断字符串首尾是否合格 下面是中间部分
if(0 != i && ch[i+1] != '\0' ){
if(('X' == ch[i] || 'x' == ch[i]) && !isdigit(ch[i-1]) && '+' != ch[i-1] && '-' != ch[i-1] && '*' != ch[i-1] && '/' != ch[i-1] && '.' != ch[i-1])
return 0;
if(('X' == ch[i] || 'x' == ch[i]) && '^' != ch[i+1] && '+' != ch[i+1] && '-' != ch[i+1] && '*' != ch[i+1] && '/' != ch[i+1])
return 0;
if(('+' == ch[i] || '-' == ch[i] || '*' == ch[i] || '/' == ch[i]) && !isdigit(ch[i-1]) && 'X' != ch[i-1] && 'x' != ch[i-1] && !isdigit(ch[i+1]) && 'X' != ch[i+1] && 'x' != ch[i+1])
return 0;
if('^' == ch[i] && 'X' != ch[i-1] && 'x' != ch[i-1])
return 0;
if('^' == ch[i] && !isdigit(ch[i+1]))
return 0;
if('.' == ch[i] && !isdigit(ch[i+1]) && !isdigit(ch[i-1]))
return 0;
}
}
}
return 1;
}

void createPoly(polynode **P, char ch[]){
char *t = ch;
int i = 0, j = 1;
int iscoef = 1,isminus = 1;
polyptr Q,L;

if('-' == ch[0]){
isminus = -1;
*t++;
}
while('\0' != *t){
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = 1;
Q->exp = 0;
Q->next = NULL;//申请节点,初始化参数为1.

if(-1 == isminus){
Q->coef *= isminus;
isminus = 1;
}

while('+' != *t && '-' != *t && '*' != *t && '/' != *t && '\0' != *t){
if('x' != *t && 'X' != *t){
while(isdigit(*t)){
i =((int)*t - 48) + i*10;
t++;
j *= i;
}//抽取数字
if(1 == iscoef && 0 != i){
Q->coef *= i;
}
if(0 == iscoef){
Q->exp += i;
iscoef = 1;
}
//如果i=0,则

}
else{
iscoef = 0;
t++;
if('^' == *t)
t++;
else
Q->exp = 1;
}
i = 0;
}//while 遍历到加减乘除,则退出循环,到下一新的节点
iscoef = 1;
if('\0' != *t){
if('-' == *t)
isminus = -1;
t++;
}
if(0 == j){
Q->coef = 0;
j = 1;
}
printf("系数:%d,指数:%d\n",Q->coef,Q->exp);

if(NULL == *P){
*P = Q;
}
else{
L->next = Q;
}
L = Q;

}//while遍历整个字符串

}
void polyAdd(polynode *A,polynode *B){
polyptr P = A, Q,L;
polyptr COPYA = NULL,COPYB = NULL;
if(NULL == A || NULL == B){
printf("多项式未被建立,请先输入多项表达式.\n");
return ;
}

while(NULL != P){//复制A
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYA)
COPYA = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
P = B;
while(NULL != P){//复制B
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYB)
COPYB = Q;
else
L->next = Q;
L = Q;
P = P->next;
}

L->next = COPYA;//把COPYA,COPYB两个多项式连接起来,整理一下就OK了.
order(©B);
order(©B);
printf("相加结果为:");
display(COPYB);
destroy(©B);
}
void polyMinus(polynode *A,polynode *B){//相减和相加差不多
polyptr P = A, Q,L;
polyptr COPYA = NULL,COPYB = NULL;
if(NULL == A || NULL == B){
printf("多项式未被建立,请先输入多项表达式.\n");
return ;
}

while(NULL != P){//复制A
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYA)
COPYA = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
P = B;
while(NULL != P){//复制B
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = -(P->coef);
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYB)
COPYB = Q;
else
L->next = Q;
L = Q;
P = P->next;
}

L->next = COPYA;//把COPYA,COPYB两个多项式连接起来,整理一下就OK了.
order(©B);
order(©B);
printf("相减结果为:");
display(COPYB);
destroy(©B);

}
void polyMulti(polynode *A,polynode *B){
polyptr R = A, P = B, Q, L = NULL, T;

if(NULL == A || NULL == B){
printf("多项式未被建立,请先输入多项表达式.\n");
return ;
}
if(0 == A->coef || 0 == B->coef){
printf("多项式乘积为:0\n");
return ;
}

while(NULL != R){
while(NULL != P){
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef * R->coef;
Q->exp = P->exp + R->exp;
Q->next = NULL;

if(NULL == L)
L = Q;
else
T->next = Q;
T = Q;
P = P->next;
}
P = B;
R = R->next;
}
order(&L);
order(&L);
printf("多项式乘积为:\n");
display(L);
destroy(&L);
}
void polyDiv(polynode *A,polynode *B){//多项式除法
polyptr P = A, Q,L,R,T,C,D;
polyptr COPYA = NULL,COPYB = NULL;
if(NULL == A || NULL == B){
printf("多项式未被建立,请先输入多项表达式.\n");
return ;
}
if(A->coef == 0){
printf("0.\n");
return ;
}
if(B->coef == 0){
printf("除数为0,错误!\n");
return ;
}
if(A->coef < B->coef){
printf("商:0,余数为:");
display(A);
return ;
}
while(NULL != P){//复制A
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef;
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYA)
COPYA = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
P = B;
while(NULL != P){//复制B
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = -(P->coef);
Q->exp = P->exp;
Q->next = NULL;
if(NULL == COPYB)
COPYB = Q;
else
L->next = Q;
L = Q;
P = P->next;
}
C = P = Q = L = R = T = NULL;

//------------------开始计算
while(COPYA->exp >= COPYB->exp){
D = COPYA;
while(NULL != D->next)
D = D->next;
R = COPYB;
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = (-COPYA->coef) / R->coef;
Q->exp = COPYA->exp - R->exp;
Q->next = NULL;
if(NULL == L)
L = Q;
else
P->next = Q;
P = Q;

while(NULL != R){
Q = (polyptr)malloc(sizeof(polynode));
Q->coef = P->coef * R->coef;
Q->exp = P->exp + R->exp;
Q->next = NULL;

if(NULL == T)
T = Q;
else
C->next = Q;
C = Q;

R = R->next;
}
D->next = T;
order(©A);
order(©A);
T = NULL;
C = NULL;
}
order(&L);
order(©A);
printf("A除以B,商:");
display(L);
printf("余数:");
display(COPYA);

destroy(&L);
destroy(©A);
destroy(©B);
}

void display(polynode *P){
//考虑情况有系数为1,指数为1,0,一般数;系数为系数不为1,指数为1,0,一般数;
//系数为负数,指数为1,0,一般数,主要考虑中间符号问题.
if(NULL == P){
printf("多项式为空.\n");
return ;
}
if(1 == P->coef){
if(0 == P->exp)
printf("1");
else if(1 == P->exp) printf("x");
else printf("x^%d",P->exp);
}
else{
if(-1 == P->coef){
if(0 == P->exp)
printf("-1");
else if(1 == P->exp) printf("-x");
else printf("-x^%d",P->exp);
}
else if(0 == P->exp)
printf("%d",P->coef);
else if(1 == P->exp) printf("%dx",P->coef);
else
printf("%dx^%d",P->coef,P->exp);
}
P = P->next;
while(NULL != P){
if(0 < P->coef){
if(1 == P->coef){
if(0 == P->exp)
printf("+1");
else if(1 == P->exp) printf("+x");
else printf("+x^%d",P->exp);
}
else{
if(0 == P->exp)
printf("+%d",P->coef);
else if(1 == P->exp) printf("+%dx",P->coef);
else
printf("+%dx^%d",P->coef,P->exp);
}
}
else{
if(-1 == P->coef){
if(0 == P->exp)
printf("-1");
else if(1 == P->exp) printf("-x");
else printf("-x^%d",P->exp);
}
else{
if(0 == P->exp)
printf("%d",P->coef);
else if(1 == P->exp) printf("%dx",P->coef);
else
printf("%dx^%d",P->coef,P->exp);
}
}
P = P->next;
}
printf("\n");
}

void destroy(polynode **P){
polyptr Q = *P;
if(NULL == *P)
return ;
while(*P != NULL){
Q = *P;
*P = (*P)->next;
delete Q;
}

}

void order(polynode **P){
//首先 系数为零的要清掉,其次指数从高到低排序,再者系数相同的要合并.
polyptr prev,curr,OUT,INcurr;//前一节点和当前节点
int temp;

//出去第一节点系数为0的项
while(NULL != *P){
if(0 != (*P)->coef)
break;
else{
if(NULL == (*P)->next)
return;
curr = *P;
(*P) = (*P)->next;
delete curr;
}
}
if(NULL == *P || NULL == (*P)->next)//如果只剩1项或空,则不需要整理,退出函数
return;
//冒泡排序
OUT = INcurr = *P;
while(NULL != OUT->next){//外循环
while(NULL != INcurr->next){//内循环
prev = INcurr;
INcurr = INcurr->next;
if(prev->exp < INcurr->exp){
temp = prev->coef;
prev->coef = INcurr->coef;
INcurr->coef = temp;//交换系数

temp = prev->exp;
prev->exp = INcurr->exp;
INcurr->exp = temp;//交换指数
}
}
OUT = OUT->next;
INcurr = *P;
}

//去除0项
prev = curr = *P;
curr = curr->next;
while(NULL != curr){
if(0 == curr->coef){
prev->next = curr->next;
delete curr;
curr = prev->next;
}
else{
prev = curr;
curr = curr->next;
}
}
//合并同类项
OUT = INcurr = *P;
while(NULL != OUT->next){
while(NULL != INcurr->next){
prev = INcurr;
INcurr = INcurr->next;
if(INcurr->exp == OUT->exp){
OUT->coef += INcurr->coef;
prev->next = INcurr->next;
delete INcurr;
INcurr = prev;
}
}
INcurr = OUT = OUT->next;
if(NULL == OUT)
return;
}

}

void main(){
int choice;
// int i;
char ch[100];
polynode *polyA,*polyB;

polyA = polyB = NULL;

menu();
scanf("%d",&choice);
while(!IsChoice(choice)){
menu();
printf("输入错误,重新输入.\n");
scanf("%d",&choice);
}
while(8 != choice){
switch(choice){
case 1:
if(NULL != polyA || NULL != polyB){
destroy(&polyA);
destroy(&polyB);
printf("原多项式被销毁.\n");
}
printf("多项式输入格式:4x^3+7x^2+x+6--x不分大小写.\n输入多项式A:\n");
scanf("%s",&ch);

while(!isPut(ch)){
printf("输入错误!重新输.\n");
scanf("%s",&ch);
}
createPoly(&polyA,ch);//建立多项式A链表

printf("输入多项式B:\n");
scanf("%s",&ch);
while(!isPut(ch)){
printf("输入错误!重新输.\n");
scanf("%s",&ch);
}
createPoly(&polyB,ch);//建立多项式B链表
order(&polyB);
order(&polyA);//整理排序多项式,默认降幂

printf("建立多项式成功!多项式:\nA为:");
display(polyA);
printf("B为:");
display(polyB);
break;
case 2:
polyAdd(polyA,polyB);
break;
case 3:
polyMinus(polyA,polyB);
break;
case 4:
polyMulti(polyA,polyB);
break;
case 5:
printf("PS:系数只支持整数,计算除法存在误差;\n如果除数所有项系数为1,能得到正确答案,或者某些情况系数刚好被整除.");
polyDiv(polyA,polyB);
break;
case 6:
printf("------显示多项式------\nA :");
display(polyA);
printf("B :");
display(polyB);
break;
case 7:
destroy(&polyA);
destroy(&polyB);
printf("此多项式已被清空.\n");
break;
default:
return ;
}
choice = 0;
menu();
scanf("%d",&choice);
while(!IsChoice(choice) || 0 == choice){
menu();
printf("输入错误,重新输入.\n");
scanf("%d",&choice);
}
}
}
百度网友da8a46e
推荐于2017-09-23 · TA获得超过7293个赞
知道小有建树答主
回答量:2307
采纳率:93%
帮助的人:188万
展开全部
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
float xi;
int n;
struct node *next;
}term;
void sort(term *head)
{
term *p,*q,*s;
p=(term *)malloc(sizeof(term));
q=(term *)malloc(sizeof(term));
s=(term *)malloc(sizeof(term));
p=head;

while(p->next!=NULL)
{
s=p->next;
while(s!=NULL)
{
if(p->n>s->n)
{
q->n=p->n;
p->n=s->n;
s->n=q->n;
q->xi=p->xi;
p->xi=s->xi;
s->xi=q->xi;
}
s=s->next;
}
p=p->next;
}
}
term *polynadd(term *head1,term *head2)
{
term *p,*q,*s,*r,*head3;
float x;
p=head1->next;
q=head2->next;
head3=(term *)malloc(sizeof(term));
r=head3;
while(p!=NULL&&q!=NULL)
{
if(p->n==q->n)
{
x=p->xi+q->xi;
if(x!=0)
{
s=(term *)malloc(sizeof(term));
s->xi=x;
s->n=p->n;
r->next=s;
r=s;
}
p=p->next;
q=q->next;
}
else if(p->n>q->n)
{
s=(term *)malloc(sizeof(term));
s->n=q->n;
s->xi=q->xi;
r->next=s;
r=s;
q=q->next;
}
else
{
s=(term *)malloc(sizeof(term));
s->n=p->n;
s->xi=p->xi;
r->next=s;
r=s;
p=p->next;
}
}
while(p!=NULL)
{
s=(term *)malloc(sizeof(term));
s->n=p->n;
s->xi=p->xi;
r->next=s;
r=s;
p=p->next;
}
while(q!=NULL)
{
s=(term *)malloc(sizeof(term));
s->n=q->n;
s->xi=q->xi;
r->next=s;
r=s;
q=q->next;
}
r->next=NULL;
return head3;

}
term *createpolyn(int m)
{

term *p,*head,*q;
int i;
head=(term *)malloc(sizeof(term));
q=head;
for(i=0;i<m;i++)
{
p=(term *)malloc(sizeof(term));
printf("请输入第%d项数的系数和指数\n",i+1);
scanf("%f%d",&p->xi,&p->n);
q->next=p;
q=p;
}
p->next=NULL;
return head;

}
dayin(term *head)
{
term *p,*q;
q=head->next;
if(q->xi==0)
printf(" ");
if(q->n==0&&q->xi>0&&q->xi!=1)
printf("%0.2f",q->xi);
if(q->n==0&&q->xi==1)
printf("1");
if(q->n==0&&q->xi<0)
printf("%0.2f",q->xi);
if(q->n==1&&q->xi==1)
printf("x");
if(q->n==1&&q->xi!=1&&q->xi>0)
printf("%0.2fx",q->xi);
if(q->n==1&&q->xi!=1&&q->xi<0)
printf("%0.2fx",q->xi);
if(q->n!=1&&q->n!=0&&q->xi>0&&q->xi!=1)
printf("%0.2fx^%d",q->xi,q->n);
if(q->n!=1&&q->n!=0&&q->xi<0)
printf("%0.2fx^%d",q->xi,q->n);
if(q->n!=0&&q->n!=1&&q->xi==1)
printf("x^%d",q->n);
p=q->next;
while(p!=NULL)
{
if(p->xi==0)
printf(" ");
if(p->n==0&&p->xi>0&&p->xi!=1)
printf("+%0.2f",p->xi);
if(p->n==0&&p->xi==1)
printf("+1");
if(p->n==0&&p->xi<0)
printf("%0.2f",p->xi);
if(p->n==1&&p->xi==1)
printf("+x");
if(p->n==1&&p->xi!=1&&p->xi>0)
printf("+%0.2fx",p->xi);
if(p->n==1&&p->xi!=1&&p->xi<0)
printf("%0.2fx",p->xi);
if(p->n!=1&&p->n!=0&&p->xi>0&&p->xi!=1)
printf("+%0.2fx^%d",p->xi,p->n);
if(p->n!=1&&p->n!=0&&p->xi<0)
printf("%0.2fx^%d",p->xi,p->n);
if(p->n!=0&&p->n!=1&&p->xi==1)
printf("+x^%d",p->n);
p=p->next;
}
printf("\n");
}
main()
{
int i,j;
term *head1,*head2,*head3;
printf("请输入第一个多项式的项数:\n");
scanf("%d",&i);
head1=createpolyn(i);
sort(head1);
printf("\n");
dayin(head1);
printf("\n");
printf("请输入第二个多项式的项数:\n");
scanf("%d",&j);
head2=createpolyn(j);
sort(head2);
printf("\n");
dayin(head2);
printf("\n\n");
head3=polynadd(head1,head2);
sort(head3);
printf("\n");
dayin(head3);
printf("\n");
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
蕙欣114
2009-11-12
知道答主
回答量:21
采纳率:0%
帮助的人:0
展开全部
scanf("%f %d",&coef,&exp);
ptr->coef = coef;
ptr->exp = exp;
ptr->next=NULL;
if(head=NULL)
head=ptr;
else
tail=ptr;
}while(n>i);
return head;
}
应改为
scanf("%f %d",&coef,&exp);
ptr->coef = coef;
ptr->exp = exp;
ptr->next=NULL;
if(head=NULL)
{head=ptr; return head;}
else
{ tail=ptr; return tail;}
}while(n>i);
}
你现在试试看啊
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
湛镶彤Sc
2019-03-09 · TA获得超过3989个赞
知道大有可为答主
回答量:3149
采纳率:30%
帮助的人:467万
展开全部
#include <stdio.h>
#include <malloc.h>
struct SLL
{
// ax^r
int a;
int r;
struct SLL* next;
};
struct SLL *add(struct SLL *head, int a, int r)
{
struct SLL *t;
struct SLL *tt;
for(t=head, tt=NULL; t; tt = t, t = t->next) if(t->r <= r) break;
if(!t)
{
struct SLL *node = (struct SLL*)malloc(sizeof(struct SLL));
node->a = a;
node->r = r;
node->next = NULL;
if(!tt) return node;
tt->next = node;
return head;
}
if(t->r == r)
{
t->a += a;
if(t->a == 0)
{
if(!tt)
{
tt = head;
head = head -> next;
free(tt);
return head;
}
tt->next = t->next;
free(t);
return head;
}
}
if(t->r < r)
{
struct SLL *node = (struct SLL*)malloc(sizeof(struct SLL));
node->a = a;
node->r = r;
node->next = t;
if(!tt) return node;
tt->next = node;
}
return head;
}
void print(struct SLL *head)
{
struct SLL *t = head;
for(; t; t=t->next)
{
printf("%d %d\n", t->a, t->r);
}
}
int main()
{
struct SLL *head = NULL;
int m, n, a, r, i;
scanf("%d%d", &m, &n);
for(i=0; i<m+n; i++)
{
scanf("%d %d", &a, &r);
head = add(head, a, r);
}
print(head);
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式