PTA乙级-1004 成绩排名 读入 n(>0)名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。

/* 用结构体实现 */
#include <stdio.h>

typedef struct
{  /* 最多10个字符加上'\0' */
    char name[11];
    char id[11];
    int  score;
}Stu;

int main ()
{   
  int i = 0;
  int val = 0;
  Stu max, min, temp;
  max.score = -1;
  min.score = 101;

  if(scanf("%d", &val)!=EOF)
  {
    for (i=0; i<val; i++)
    {
        if(scanf ("%s %s %d", temp.name, temp.id, &temp.score)!=EOF)
        {
          if (temp.score < min.score)
          {
              min = temp;
          } 
          if (temp.score > max.score)
          {
              max = temp;
          } 
        }
        else
          return -1;
    }
      printf ("%s %s\n",max.name, max.id);
      printf ("%s %s\n",min.name, min.id);
      return 0;
  }
  return -1;
}

猜你喜欢

转载自blog.csdn.net/weixin_46809332/article/details/106026052