解题思路: 这数据输入,每行的最后一个字符是'\n',但是最后一行的最后一个字符是啥不知道,反正不是空格,也不是换行。
注意事项: 贴的代码乱乱的,去这看吧:https://www.dotcpp.com/run/13388639
参考代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct _node{
int value;
int factor;
struct _node *next;
}node;
void add_one(node **phead,int v,int f);
void show(node *head);
void free_all(node **phead);
void together(node **phead1,node **phead2);
int main()
{
node *head1 = NULL;
node *head2 = NULL;
int v,f;char isend;
int first;
while(~scanf("%d %d",&v,&f))
{
isend = getchar();
first = 0;
do{
if(first)
{
scanf("%d %d",&v,&f);
isend = getchar();
}
else first = 1;
add_one(&head1,v,f);
}while(isend!='\n');
isend = '\0';
while(isend!='\n')
{
scanf("%d %d",&v,&f);
isend = getchar();
add_one(&head2,v,f);
if(isend!=' ') break;
}
together(&head1,&head2);
free_all(&head1);
}
return 0;
}
void add_one(node **phead,int v,int f)
{
node *p = *phead;
node *a = (node*)malloc(sizeof(node));
a->value = v;a->factor = f;
a->next = NULL;
if(!p) *phead = a;
else
{
while(p->next)
p=p->next;
p->next = a;
}
}
void show(node *head)
{
node *p = head;
while(p->next)
{
if(p->value!=0)
printf("%d %d ",p->value,p->factor);
p=p->next;
}
if(p->value!=0)
printf("%d %d \n",p->value,p->factor);
}
void free_all(node **phead)
{
node *p = *phead;
while(p)
{
node *rem = p;
p=p->next;
free(rem);
}
*phead = NULL;
}
void together(node **phead1,node **phead2)
{
node *p1 = NULL;
node *p2 = NULL;
if((*phead1)->factor>=(*phead2)->factor)
{
p1 = *phead1;
p2 = *phead2;
}
else
{
p1 = *phead2;
p2 = *phead1;
}
node *p1copy = p1;
node *p2next = NULL;
node *p1pre = NULL;
int isok = 1;
while(p2 && isok)
{
while(p1->factor>p2->factor)
{
p1pre = p1;
p1 = p1->next;
if(!p1)
{
p1pre->next = p2;
isok = 0;
break;
}
}
if(isok)
{
if(p1->factor==p2->factor)
{
p1->value += p2->value;
node *buf = p2;
p2 = p2->next;
free(buf);
}
else
{
p2next = p2->next;
p1pre->next = p2;
p2->next = p1;
p2 = p2next;
}
}
}
show(p1copy);
*phead2 = NULL;
}
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题7.4 (C语言代码)浏览:594 |
程序员的表白 (C语言代码)浏览:1575 |
数组输出 (C语言代码)浏览:811 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:746 |
C语言程序设计教程(第三版)课后习题10.1 (Java代码)浏览:1492 |
C语言训练-大、小写问题 (C语言代码)浏览:792 |
printf基础练习2 (C语言代码)浏览:690 |
C语言程序设计教程(第三版)课后习题7.2 (C语言代码)浏览:570 |
C语言程序设计教程(第三版)课后习题8.7 (C语言代码)浏览:609 |
三角形 (C语言代码)浏览:965 |