私信TA

用户名:uq_60654334766

访问量:294

签 名:

等  级
排  名 613
经  验 4022
参赛次数 0
文章发表 6
年  龄 0
在职情况 在职
学  校
专  业

  自我简介:

TA的其他文章

解题思路:  这数据输入,每行的最后一个字符是'\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 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区