#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
//递归函数求n个字符串中的最大和最小字符串
void MaxMinStr(int StrNums,char StrArr[][50],char **MaxStr,char MinStr[50])
{
if(StrNums>0)
{
*MaxStr=StrArr[0];
MinStr=StrArr[0];
if(strcmp(*MaxStr,StrArr[StrNums-1])<0){
*MaxStr=StrArr[StrNums-1];
printf("max:%s\n",*MaxStr);
}
if(strcmp(MinStr,StrArr[StrNums-1])>0){
MinStr=StrArr[StrNums-1];
printf("min:%s\n",MinStr);
}
printf("max:%s min:%s\n",*MaxStr,MinStr);
MaxMinStr(StrNums-1,StrArr,MaxStr,MinStr);
}
}
void InputStr(char StrArr[][50],int StrNums)
{
for(int i=0;i<StrNums;i++)
{
scanf("%s",StrArr[i]);
}
}
void PrintStr(char StrArr[][50],int StrNums)
{
for(int i=0;i<StrNums;i++)
{
printf("%s ",StrArr[i]);
}
printf("\n");
}
int main(void)
{
char StrArr[100][50];
char *MaxStr=NULL;
char MinStr[50];
int n;
scanf("%d",&n);
InputStr(StrArr,n);
MaxStr=StrArr[0];
strcpy(MinStr,StrArr[0]);
MaxMinStr(n,StrArr,&MaxStr,MinStr);
PrintStr(StrArr,n);
printf("%s : %s\n",MaxStr,MinStr);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
typedef struct MathsContestAchievements
{
int Numbers;//参赛者号码
char Name[20];//参赛者姓名
int Achievements;//比赛得分
struct MathsContestAchievements* Next;
}MCA;
//按输入顺序创建有n个节点的参赛者信息链表
MCA* CreateMCAList(int n)
{
MCA*pHead=NULL;
MCA*pRear=NULL;
MCA*pCurr=NULL;
for(int i=0;i<n;i++)
{
pCurr=(MCA*)(malloc(sizeof(MCA)));
scanf("%d",&(pCurr->Numbers));
scanf("%s",pCurr->Name);
scanf("%d",&(pCurr->Achievements));
pCurr->Next=NULL;
if(i==0)
{
pHead=pCurr;
pRear=pCurr;
}
else
{
pRear->Next=pCurr;
pRear=pCurr;
}
}
return pHead;
}
//打印链表
void PrintMCAList(MCA* pHead)
{
if(pHead!=NULL)
{
printf("%4d %-20s %3d\n",pHead->Numbers,pHead->Name,pHead->Achievements);
PrintMCAList(pHead->Next);
}
}
//原链表移走最高最低分者,最高最低分者分别构成新链表
void ToMoveHighestAndLowest(MCA *pAfterMoveHead,MCA* pHighestHead,MCA *pLowestHead,int n,MCA *pHead)
{
MCA *p,*p0,*r,*r0,*q,*q0;
int max=0;
int min=1000;
p=pHead;
while(p!=NULL){
if(p->Achievements>max){
max=p->Achievements;
}
if(p->Achievements<min){
min=p->Achievements;
}
p=p->Next;
}
p=pHead;
while(p!=NULL){
if(p->Achievements==max){
if(p==pHead){
pHead=p->Next;
q=p;
pLowestHead=q;
pLowestHead=NULL;
}else{
p0->Next=p->Next;
q0=p;
q->Next=q0;
q0->Next=NULL;
q=q0;
p=p0;
}
}
if(p->Achievements==min){
if(p==pHead){
pHead=p->Next;
q=p;
pHighestHead=q;
pHighestHead=NULL;
}else{
p0->Next=p->Next;
q0=p;
q->Next=q0;
q0->Next=NULL;
q=q0;
p=p0;
}
}
p0=p;
p=p->Next;
}
}
//删除链表所有节点
void DeleteMCAList(MCA* pHead)
{
if(pHead!=NULL)
{
MCA* pDel;
pDel=pHead;
pHead=pHead->Next;
free(pDel);
DeleteMCAList(pHead);
}
}
int main(void)
{
MCA* pHead=NULL;//原链表头指针
int n;//原链表节点数
MCA* pAfterMoveHead=NULL;//去掉最高最低分后的链表头指针
MCA* pHighestHead=NULL;//最高分链表头指针
MCA* pLowestHead=NULL;//最低分链表头指针
scanf("%d",&n);
pHead=CreateMCAList(n);//创建原链表
//原链表移走最高最低分参赛者,最高分和最低分者分别构成新链表
ToMoveHighestAndLowest(pAfterMoveHead,pHighestHead,pLowestHead,n,pHead);
printf("After Being Moved:\n");
PrintMCAList(pAfterMoveHead);//打印移走最高最低分后的链表
printf("Highest:\n");
PrintMCAList(pHighestHead);//打印最高分链表
printf("Lowest:\n");
PrintMCAList(pLowestHead);//打印最低分链表
DeleteMCAList(pAfterMoveHead);//释放链表节点空间
DeleteMCAList(pHighestHead);//释放链表节点空间
DeleteMCAList(pLowestHead);//释放链表节点空间
pHead=NULL;
pAfterMoveHead=NULL;
pHighestHead=NULL;
pLowestHead=NULL;
return 0;
}