求解 这题用C语言怎么做

2025-04-30 20:05:18
推荐回答(2个)
回答1:

#include
#include
#include
struct node
{
    int data;
    char s[100];
    struct node *next;
};
int main()
{
    char c;
    char str[100];
    int n,i,j,flag;
    struct node *head,*tail,*p,*q;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        p=(struct node*)malloc(sizeof(struct node));
        scanf("%s%d",p->s,&p->data);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    while(~scanf("%c",&c)&&c!='O')
    {
        if(c=='A')
        {
            flag=0;
            q=(struct node*)malloc(sizeof(struct node));
            scanf("%s%d",q->s,&q->data);
            q->next=NULL;
            p=head;
            while(p->next!=NULL)
            {
                if(p->next->data>=q->data)
                {
                    p=p->next;
                }
                else
                    break;
            }
            q->next=p->next;
            p->next=q;
        }
        if(c=='Q')
        {
            scanf("%s",str);
            tail=head;
            p=head->next;
            while(p!=NULL)
            {
                if(strcmp(p->s,str)==0)
                {
                    tail->next=p->next;
                    free(p);
                    break;
                }
                tail=p;
                p=p->next;
            }
        }
        if(c=='C')
        {
            q=(struct node*)malloc(sizeof(struct node));
            scanf("%s%d",q->s,&q->data);
            q->next=NULL;
            tail=head;
            p=head->next;
            while(p!=NULL)
            {
                if(strcmp(p->s,q->s)==0)
                {
                    q->data+=p->data;
                    tail->next=p->next;
                    break;
                }
                tail=p;
                p=p->next;
            }
            p=head;
            while(p->next!=NULL)
            {
                if(p->next->data>=q->data)
                {
                    p=p->next;
                }
                else
                    break;
            }
            q->next=p->next;
            p->next=q;
        }
        if(c=='S')
        {
            p=head->next;
            while(p!=NULL)
            {
                printf("%s %d\n",p->s,p->data);
                p=p->next;
            }
            printf("\n");
        }
        if(c=='O')
            break;
    }
    j=1;
    p=head->next;
    int m=p->data;
    while(j!=7)
    {
        if(j==1)
            printf("#1 :");
        if(j==2)
            printf("\n#2 :");
        if(j==4)
            printf("\n#3 :");
        while(m==p->data)
        {
            printf(" %s",p->s);
            p=p->next;
        }
        m=p->data;
        j++;
    }
    printf("\n");
    return 0;
}

 听说回答要够长才能够自动采纳

回答2:

#include
#include
#include
struct code
{
char name[20];
int g;
struct code *next;
};

int main()
{
int n,i;
char pl;
struct code *head,*p,*tail,*be,*q;
scanf("%d",&n);
head=(struct code *)malloc(sizeof(struct code));
head->next=NULL;
tail=head;
for(i=0;i {
p=(struct code *)malloc(sizeof(struct code));
p->next=NULL;
scanf("%s%d",p->name,&p->g);
tail->next=p;
tail=p;
}

while(1)
{
scanf("%c",&pl);
if(pl=='O')
{
break;
}
if(pl=='A')
{
p=(struct code *)malloc(sizeof(struct code));
p->next=NULL;
scanf("%s%d",p->name,&p->g);
be=head;
q=be->next;
while(q!=NULL)
{
if(p->g>q->g)break;
be=q;q=q->next;
}
p->next=q;
be->next=p;
}
if(pl=='Q')
{
p=(struct code *)malloc(sizeof(struct code));
p->next=NULL;
scanf("%s",p->name);
be=head;
q=be->next;
while(q!=NULL)
{
if(strcmp(p->name,q->name)==0)
break;
be=q;q=q->next;
}
be->next=q->next;
free(q);
free(p);
}
if(pl=='C')
{
p=(struct code *)malloc(sizeof(struct code));
p->next=NULL;
scanf("%s%d",p->name,&p->g);
be=head;
q=be->next;
while(q!=NULL)
{
if(strcmp(p->name,q->name)==0)
break;
be=q;q=q->next;
}
p->g+=q->g;
be->next=q->next;
free(q);
be=head;
q=be->next;
while(q!=NULL)
{
if(p->g>q->g)break;
be=q;q=q->next;
}
p->next=q;
be->next=p;
}
if(pl=='S')
{
p=head->next;
while(p!=NULL)
{
printf("%s %d\n",p->name,p->g);
p=p->next;
}
printf("\n");
}
}
p=head->next;tail=p;
int v=0;
printf("#1 :");
for(i=0;i<1;i++)
{
printf(" %s",tail->name);
tail=tail->next;
}
p=tail;
while(p->g==p->next->g)
{
v++;
p=p->next;
}
for(i=0;i {
printf(" %s",tail->name);
tail=tail->next;
}printf("\n");
printf("#2 :");
for(i=0;i<2;i++)
{
printf(" %s",tail->name);
p=tail;
tail=tail->next;
}
v=0;
while(p->g==p->next->g)
{
v++;
p=p->next;
}
for(i=0;i {
printf(" %s",tail->name);
tail=tail->next;
}printf("\n");//2222
v=0;
printf("#3 :");
for(i=0;i<3;i++)
{
printf(" %s",tail->name);
p=tail;
tail=tail->next;
}
while(p->g==p->next->g)
{
v++;
p=p->next;
}
for(i=0;i {
printf(" %s",tail->name);
tail=tail->next;
}printf("\n");
return 0;
}