/SDUT 2057 金牌 银牌 铜牌 记录这个题之前先发表一下感受,这个题的整体思路不难,但实现起来不是很容易,至少对我来说。输出排名的函数一开始没写出代码来,受同学的启发,写出来,提交成功了,特意记录一下。 做完这个题之后,我发现在链表中排序可以作为一个模板,这个功能做题时用到不少。收藏这个模板,在以后 做题时会省很多时间。 以下是源码:/
#include <stdio.h> #include <stdlib.h> #include <string.h>
struct Node { char Name[22]; int score; struct Node *next; };
void show (struct Node *x);//显示 struct Node *Creat(int n); //创建 struct Node *Add(struct Node *shead,struct Node *x);//增加 struct Node *Delete(struct Node * x,char str[]);//删除 struct Node *Change(struct Node *x,char str[],int score);//改变 void FindKing(struct Node *x);//找排名
struct Node *Sort(struct Node *x);//排序
int main () { struct Node *head,*p; int n,score; char ch,str[22]; scanf ("%d",&n); head=Creat(n); while ((scanf (" %c",&ch))!=EOF) { if (ch==‘A’) { p=(struct Node *)malloc (sizeof (struct Node)); if (p= =NULL) exit (0); p->next=NULL; scanf ("%s%d",p->Name ,&p->score ); head=Add(head,p); } else if (ch==‘Q’) { scanf ("%s",str); head=Delete(head,str); } else if (ch==‘C’) { scanf ("%s %d",str,&score); head=Change(head,str,score); } else if (ch==‘S’) { Sort(head); show (head); } else if (ch==‘O’) { Sort(head); FindKing(head); exit(0); } } return 0; } // 创建 struct Node *Creat(int n) { struct Node *head,*p,*q; int i; head=(struct Node *)malloc (sizeof (struct Node)); if (head= =NULL) exit (0); scanf ("%s%d",head->Name,&head->score ); head->next =NULL; p=head; q=head; for (i=0;i<n-1;i++) { p=(struct Node *)malloc (sizeof (struct Node )); if (p==NULL) exit(0); p->next =NULL; scanf ("%s%d",p->Name,&p->score ); q->next =p; q=p; } return head; } //排序 struct Node *Sort(struct Node *x) { struct Node *p,*q,*head; char str[22]; int temp; p=head=x; q=p->next ; while (p->next!=NULL) { while (q!=NULL) { if(p->score score ) { temp=p->score ; p->score =q->score ; q->score =temp; strcpy(str,p->Name ); strcpy(p->Name ,q->Name ); strcpy(q->Name ,str); } q=q->next ; } p=p->next ; q=p->next; } return head; } //显示 void show (struct Node *x) { struct Node *p,*q; char str[20]; int temp; p=x; while (p!=NULL) { printf ("%s %d\n",p->Name ,p->score ); p=p-> next; } } //增加,加到最后一个与他分数相同的学生后面,一个为头结点,一个为新来节点 struct Node *Add(struct Node *shead,struct Node *x) { struct Node *head,*p,*q; head=shead; p=shead; while (x->score !=p->score&&p->next !=NULL) { p=p->next ; }//此时p->score与新来的学生score一样,或者p为最后一个元素 if(p->next !=NULL)//有相同元素,分两种情况 { while (p->score ==x->score &&p->next !=NULL) { q=p; p=p->next ; } if(p->next ==NULL&&p->score ==x->score )//最后一个是相同元素 { p->next =x; } else//最后一个是不同元素,前面是相同元素 { x->next =p; q->next =x; } } else//无相同元素,接在最后 { p->next =x; } return head; }
//删除 struct Node *Delete(struct Node * x,char str[]) { struct Node *head,*p,*q; head=x; p=x; q=x; //分两种情况,一种是删除头节点,一种不是头节点 if(strcmp(head->Name ,str)==0) { head=p->next ; free(q); return head; } while (p->next!=NULL&&strcmp(p->Name ,str)!=0) { q=p; p=p->next ; } if( strcmp(p->Name ,str)==0) { q->next =q->next ->next ; free§; } return head; } //改变 struct Node *Change(struct Node *x,char str[],int score) { struct Node *head,*p,*q; p=q=head=x; while (p!=NULL&&strcmp(p->Name ,str)!=0) { p=p->next ; } if (strcmp(p->Name ,str)==0) { p->score +=score; } return head; }
//排名 void FindKing(struct Node *x) { struct Node *head,*p,*q; int i; int count =1; //找第一名,只要下一个与第一个一样就输出 q=x; p=q->next ; printf ("#1 : %s",q->Name ); while (p!=NULL&&p->score ==q->score ) { printf (" %s",p->Name ); p=p->next ; } printf ("\n"); //第二名,先输出两个,后面的一样就输出 q=p; p=p->next ; printf ("#2 : %s",q->Name ); for (i=0;i<1;i++) { printf (" %s",p->Name ); q=p; p=p->next ; } while (p!=NULL&&p->score ==q->score ) { printf (" %s",p->Name ); p=p->next ; } printf ("\n"); //第三名,先输出三个,后面一样就输出 q=p; p=p->next ; printf ("#3 : %s",q->Name ); for (i=0;i<2;i++) { printf (" %s",p->Name ); q=p; p=p->next ; } while (p!=NULL&&p->score ==q->score) { printf (" %s",p->Name ); p=p->next ; } }
