打dota打了一夜,睡觉。。。
#include <stdio.h>
#include <stdlib.h>
struct tagNode
{
int coef;
int exp;
struct tagNode *next;
};
typedef struct tagNode Node;
typedef struct tagNode* pNode;
void show(pNode head)
{
pNode p = head->next;
printf("y=");
while(p != NULL)
{
printf("%dx%d",p->coef,p->exp);
if(p->next != NULL)
{
printf("+");
}
p = p->next;
}
printf("\n");
}
pNode CreateList()
{
pNode head = (pNode)malloc(sizeof(Node));
head->next = NULL;
return head;
}
pNode init_node(pNode head)
{
pNode pre,cur;;
int coef,exp;
while (true)
{
scanf("%d %d",&coef,&exp);
if (coef==0&&exp==0)
{
break;
}
//遍历链表,找到合适的位置
pre = head;
cur = pre->next;
while (cur)
{
if (cur->exp >= exp)
{
break;
}
pre = cur;
cur = pre->next;
}
if (cur && cur->exp == exp) //相同的exp,合并
{
cur->coef += coef;
}
else
{
//创建新节点,并添加到链表上
pre->next = (pNode)malloc(sizeof(Node));
pre->next->coef = coef;
pre->next->exp = exp;
pre->next->next = cur;
}
}
return head;
}
void main()
{
int j;
pNode head = CreateList();
init_node(head);
show(head);
scanf("%d",&j);
}
#include <stdio.h>
#include <stdlib.h>
struct tagNode
{
int coef;
int exp;
struct tagNode *next;
};
typedef struct tagNode Node;
typedef struct tagNode* pNode;
void show(pNode head)
{
pNode p = head->next;
printf("y=");
while(p != NULL)
{
printf("%dx%d",p->coef,p->exp);
if(p->next != NULL)
{
printf("+");
}
p = p->next;
}
printf("\n");
}
pNode CreateList()
{
pNode head = (pNode)malloc(sizeof(Node));
head->next = NULL;
return head;
}
pNode init_node(pNode head)
{
pNode pre,cur;;
int coef,exp;
while (true)
{
scanf("%d %d",&coef,&exp);
if (coef==0&&exp==0)
{
break;
}
//遍历链表,找到合适的位置
pre = head;
cur = pre->next;
while (cur)
{
if (cur->exp >= exp)
{
break;
}
pre = cur;
cur = pre->next;
}
if (cur && cur->exp == exp) //相同的exp,合并
{
cur->coef += coef;
}
else
{
//创建新节点,并添加到链表上
pre->next = (pNode)malloc(sizeof(Node));
pre->next->coef = coef;
pre->next->exp = exp;
pre->next->next = cur;
}
}
return head;
}
void main()
{
int j;
pNode head = CreateList();
init_node(head);
show(head);
scanf("%d",&j);
}