ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SDUT 2057 金牌 银牌 铜牌

2019-07-18 19:43:42  阅读:247  来源: 互联网

标签:Node head struct 2057 next score SDUT 铜牌 Name


/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(p);
}

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;
//找第一名,只要下一个与第一个一样就输出
p=x;
q=p->next ;
printf ("#1 : %s",p->Name );
while (p->score ==q->score &&q->next !=NULL)
{
printf (" %s",q->Name );
p=q;
q=q->next ;
}
if(q->next ==NULL&&q->score ==p->score )
{
if (q->score ==p->score )
{
printf (" %s",q->Name );
exit (0);
}
else
{
printf ("\n#2 : %s",q->score );
exit(0);
}
}
printf ("\n");

//1 找第二名,先输出两名,若下一个一样,也输出;2 若下一个不一样,输出不够两个,再输一个,若下一个一样,也输出
p=q;
q=p->next ;
printf ("#2 : %s",p->Name );
if (q==NULL)
{
	exit(0);
}
while (p->score ==q->score &&q->next !=NULL)
{
	printf (" %s",q->Name );
	count++;
	p=q;
	q=q->next ;
}
if(q->next ==NULL )
{
	if (q->score ==p->score )
	{
		printf (" %s",q->Name );
		exit (0);
	}
	else
	{
		printf("\n#3 : %s",q->Name );
		exit(0);
	}
}
if(count<2)
{
	p=q;
	q=p->next ;
	printf (" %s",p->Name );
	while (p->score ==q->score &&q->next !=NULL)
	{
		p=q;
		q=q->next ;
		printf (" %s",p->Name );
		count++;
	}
	if(q->next ==NULL )
	{
		if (q->score ==p->score )
		{
			printf (" %s",q->Name );
			exit (0);
		}
		else
		{
			printf("\n#3 : %s",q->Name );
			exit(0);
		}
	}
}
printf ("\n");

//找第三名,先输出三名,若下一个一样,也输出
p=q;
q=q->next;	
printf ("#3 : %s",p->Name );
count=1;//代表已经输出一个了
while (p->score ==q->score &&q->next !=NULL)
{
	printf (" %s",q->Name );
	count++;
	p=q;
	q=q->next ;
}
if (count<3&&q->next !=NULL)
{
	p=q;
	q=q->next ;
	printf (" %s",p->Name );
	count++;
	if (q->next ==NULL&&count<3)
	{
		printf (" %s",q->Name );
		exit(0);
	}	
}
if (count<3&&q!=NULL)
{
	p=q;
	q=q->next ;
	printf (" %s",p->Name );
}

}

标签:Node,head,struct,2057,next,score,SDUT,铜牌,Name
来源: https://blog.csdn.net/fghfghhgjgj/article/details/96446896

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有