PAT | 1004 成绩排名 (C语言)

1004 成绩排名 (20 分)

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

输入格式:

每个测试输入包含 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

/* 用结构体实现 */
#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;
}
/* 用结构体指针实现 */
#include <stdio.h>
#include <stdlib.h>

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

int main ()
{   
  int i = 0;
  int val = 0;
  pStu max, min, temp;
  /* 申请内存 */
  max  = (pStu)malloc(sizeof(Stu));
  min  = (pStu)malloc(sizeof(Stu));
  temp = (pStu)malloc(sizeof(Stu));
  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;
              //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);
      /* 释放内存 */
      free(max);
      free(min);
      free(temp);
      /* 避免野指针 */
      max  = NULL;
      min  = NULL;
      temp = NULL; 
      
      return 0;
  }
  return -1;
}
### 关于PAT单位排行C语言测试点2的解析 #### 输入规格说明 对于每个测试案例,在一行中给出总考生数量。随后按照指定格式打印最终排名列表[^3]。 #### 准考证号结构分析 准考证号由6个字符构成,首位字母指示考试等级:`B`表示乙级,`A`表示甲级,而`T`则对应顶级。每位考生的成绩位于区间\[0, 100\]之间,并且学校通过最多含6个小写字母(忽略大小写差异)来编码识别[^4]。 #### 数据处理逻辑实现 为了完成这一任务,可以采用如下方法: - **读取输入数据**:接收并解析来自标准输入的数据流; - **构建字典存储成绩表**:创建一个字典用于保存各学校的累计分数以及参与人数; - **遍历所有记录更新统计信息**:逐条处理每一条参赛者的信息,依据所属学校累积相应分值; - **计算平均分与排序**:基于上述统计数据计算各个机构的加权评分,并据此排列名次; - **输出结果**:依照规定模板展示最终排行榜单。 以下是Python代码片段演示如何执行这些操作: ```python from collections import defaultdict def process_input(): n = int(input()) schools = defaultdict(lambda : {'total_score': 0, 'count': 0}) for _ in range(n): id_, score_str, school_code = input().split() score = int(score_str) # Update statistics for this school schools[school_code.lower()]['total_score'] += score schools[school_code.lower()]['count'] += 1 return dict(schools) def calculate_weighted_scores(school_data): weighted_scores = [] for code, stats in school_data.items(): avg_score = round(stats['total_score']/stats['count']) entry = { 'code': code, 'avg_score': avg_score, 'num_students': stats['count'] } weighted_scores.append(entry) return sorted(weighted_scores, key=lambda x:(-x['avg_score'], x['code'])) if __name__ == "__main__": data = process_input() results = calculate_weighted_scores(data) print(len(results)) for res in results: print(f"{res['code']} {res['num_students']} {res['avg_score']}") ``` 该程序首先收集所有的输入数据并将它们分类整理到不同学校下,接着计算出每个学校的平均得分及其参加的学生数目,最后按要求输出排序后的表格。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式逍遥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值