PAT (Basic Level) Practise (中文)1004. 成绩排名 (20)

读入n名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。

输入格式:每个测试输入包含1个测试用例,格式为

  第1行:正整数n
  第2行:第1个学生的姓名 学号 成绩
  第3行:第2个学生的姓名 学号 成绩
  ... ... ...
  第n+1行:第n个学生的姓名 学号 成绩

其中姓名和学号均为不超过10个字符的字符串,成绩为0到100之间的一个整数,这里保证在一组测试用例中没有两个学生的成绩是相同的。

 

输出格式:对每个测试用例输出2行,第1行是成绩最高学生的姓名和学号,第2行是成绩最低学生的姓名和学号,字符串间有1空格。

输入样例:

3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95

输出样例:

Mike CS991301
Joe Math990112


//PAT (Basic Level) Practise  1004
//create by zlc on 12/13/2014   
#include <stdio.h>
#include <stdlib.h>
struct student *create(int n);

struct student{            //个人信息结构体
    char name[11];
    char id[11];
    int score;
    struct student *next;
};

int main()
{
    int i,n;
    scanf("%d",&n);
    struct student *list;
    list=create(n);             //create linked list
    int max=0,min=101;
    struct student *p;
    for(p=list;p!=NULL;p=p->next)       //find the max and min
    {
        max=(p->score>max)?p->score:max;
        min=(p->score<min)?p->score:min;
    }

    for(p=list;p!=NULL;p=p->next)       //output
    {
        if(p->score==max)
        {
            printf("%s %s\n",p->name,p->id);
            break;
        }
    }

    for(;list!=NULL;list=list->next)
    {
        if(list->score==min)
        {
            printf("%s %s\n",list->name,list->id);
            break;
        }
    }
    return 0;
}

struct student *create(int n)           //创建链表,输入数据
{
    struct student *head,*front,*new_node;
    int i;
    for(i=0;i<n;i++)
    {
        new_node=(struct student*)malloc(sizeof(struct student));
        scanf("%s %s %d",&new_node->name,&new_node->id,&new_node->score);
        if(i==0)
            front=head=new_node;
        else
            front->next=new_node;
        new_node->next=NULL;
        front=new_node;
    }
    return(head);
}

  

posted @ 2014-12-13 18:22  笨笨小成  阅读(149)  评论(0编辑  收藏  举报