1095 解码PAT准考证(C语言)

设计思路:
  • 排好序按要求输出
qsort() :

使用总结:

  1. qsort() 默认是按照升序排序的
  2. 实现 compare(key,element) 时,若:
    • if (key < element) return -1;
    • else if (key > element) return 1;
    • else return 0;
    • 若 key,element 为整数类型,可简化为 return key - element;
    • 最终结果就是升序序列
    • 按照升序返回相反数,最终就是降序
#include <stdlib.h>
void qsort(void *base, size_t num, size_t width,
           int(*compare)(const void *key, const void *element));

base:需要排序的元素集合,通常为各类数组
num:元素集合的大小,通常为数组的长度
width:集合中元素的大小,通常为数组元素的大小,sizeof(base[0])
compare:
  - 比较函数,需要自定义,既判断快排过程中 key 和 element 的相对大小

qsort() 排序规则:
1. qsort() 默认是按照升序排序的

2. 根据 compare(key, element) 返回值,判断快排过程中 key 和 element 的大小
  - 返回值 -1(或任意负整数):key 小于 element
  - 返回值 0:key 等于 element
  - 返回值 1(或任意正整数):key 大于 element

3. 如果需要对元素降序排序,只需要 compare() 返回升序的相反数

可掌握快排算法后,再加深理解

参考:
qsort() — Sort array

编译器:C (gcc)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct id{
        char str[14];
        char level[2];
        int room;
        int date;
        int num;
        int score;
};

int cmp(const void *a, const void *b)
{
        struct id *ida = (struct id*)a, *idb = (struct id*)b;
        if (ida->score != idb->score)
                return idb->score - ida->score;
        return strcmp(ida->str, idb->str);
}

int type1(struct id ids[], int n, int i);
int type2(struct id ids[], int n, int i);
int type3(struct id ids[], int n, int i);

int main()
{
        struct id ids[10000];
        int n, m, type;
        int i;
        char level[2];

        scanf("%d %d", &n, &m);
        for (i = 0; i < n; i++) {
                scanf("%s %d", ids[i].str, &ids[i].score);
                sscanf(ids[i].str, "%1s%3d%6d%3d", ids[i].level,
                                &ids[i].room, &ids[i].date, &ids[i].num);
        }

        qsort(ids, n, sizeof(ids[0]), cmp);

        for (i = 0; i < m; i++) {
                scanf("%d", &type);
                if (type == 1)
                        type1(ids, n, i);
                else if (type == 2)
                        type2(ids, n, i);
                else if (type == 3)
                        type3(ids, n, i);
        }

        return 0;
}

int type1(struct id ids[], int n, int i)
{
        char level[2];
        int f = 1, j;
        scanf("%1s", level);
        printf("Case %d: %d %c\n", i + 1, 1, level[0]);
        for (j = 0; j < n; j++)
                if (ids[j].level[0] == level[0]) {
                        f = 0;
                        printf("%s %d\n", ids[j].str,ids[j].score);
                }
        if (f)
                puts("NA");
        return 0;
}

int type2(struct id ids[], int n, int i)
{
        int room, sum = 0, count = 0;
        int j, f = 1;

        scanf("%d", &room);
        printf("Case %d: %d %d\n", i + 1, 2, room);
        for (j = 0; j < n; j++)
                if (ids[j].room == room) {
                        f = 0;
                        count++;
                        sum += ids[j].score;
                }
        if (f)
                puts("NA");
        else
                printf("%d %d\n", count, sum);

        return 0;
}

int type3(struct id ids[], int n, int i)
{
        int date, max = 0, rooms[1000] = {0};
        int f = 1, j;

        scanf("%d", &date);
        printf("Case %d: %d %06d\n", i + 1, 3, date);
        for (j = 0; j < n; j++)
                if (ids[j].date == date) {
                        f = 0;
                        rooms[ids[j].room]++;
                        if (max < rooms[ids[j].room])
                                max = rooms[ids[j].room];
                }
        if (f)
                puts("NA");
        else
                for (; max > 0; max--)
                        for (j = 101; j < 1000; j++)
                                if (rooms[j] == max)
                                        printf("%d %d\n", j, max);

        return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值