PAT 1028 List Sorting (25分) 用char[],不要用string

问题

Excel can sort records according to any column. Now you are supposed to imitate this function.

Input Specification:
Each input file contains one test case. For each case, the first line contains two integers N (≤10​5 ) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student’s record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).

Output Specification:
For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID’s; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID’s in increasing order.

Sample Input 1:
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
Sample Output 1:
000001 Zoe 60
000007 James 85
000010 Amy 90
Sample Input 2:
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
Sample Output 2:
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Sample Input 3:
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
Sample Output 3:
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90

[Abp vNext 入坑分享] – 6.完整接入swagger

问题解读

给出N个学生信息和一个数字C,每个学生的信息包罗ID(6位数字),姓名(长度最多为8的字符串,无空格),分数(0-100)。凭据数字C的取值,对学生信息根据差别计谋举行排序,最终输出排序后的学生信息:

C=1:按 ID 递增;
C=2:按姓名递增,若是同名,按ID递增;
C=3:按分数递增,若是同分,按ID递增。

思绪剖析

这不就是三种排序计谋就完了吗?

  • 首先建立结构体 Student 保留学生信息,注重 name 字段不要用string!!!,否则你最后一个测试点会是 运行超时,现实是内存溢出!
    PAT 1028 List Sorting (25分) 用char[],不要用string
    实在可以想想,问题给出说明姓名字段长度不跨越10个字符,怎么可能没用呢,是吧!
  • 若何排序,由于我们使用sort()函数对整个结构体数组举行排序,自己实现的对照函数只能是这样int cmp(Student a, Student b),以是我们要把 C 界说成一个全局变量,然后在这个函数中凭据 C的取值举行差别的逻辑实现,固然你也可以实现三个对照函数,但我以为没有必要。

代码

问题对照简单,代码注释也挺详细,没什么好说的。有个地方需要注重:学号是6位数字,输出必须用printf("%06d", id)格式化。

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;

// 学生信息
struct Student {
    int id, score;
    // string name;
    char name[10];
}stu[10001];

// 凭据谁人字段排序
int flag;

// 自界说对照函数
int cmp(Student a, Student b) {
    // 根据ID递增
    if (flag == 1) return a.id < b.id;
    // 根据姓名自增
    else if (flag == 2) {
        // 重名就对照ID
        if (strcmp(a.name, b.name) == 0) return a.id < b.id;
        return strcmp(a.name, b.name) <= 0;
    } else {
        // 根据分数递增,相同就对照ID
        return a.score != b.score ? a.score < b.score : a.id < b.id;
    }
}

int main() {

    // N 个学生
    int n;
    // 凭据哪个字段排序
    cin >> n >> flag;
    // 读入学生信息
    for (int i = 0; i < n; ++i) {
        // cin >> stu[i].id >> stu[i].name >> stu[i].score;
        scanf("%d %s %d", &stu[i].id, stu[i].name, &stu[i].score);
    }
    // 排序
    sort(stu, stu + n, cmp);
    // 输出
    for (int i = 0; i < n; ++i) {
        // printf("%06d ", stu[i].id);
        // cout << stu[i].name << " " << stu[i].score << endl;
        printf("%06d %s %d\n", stu[i].id, stu[i].name, stu[i].score);
    }

    return 0;
}

原创文章,作者:28x29新闻网,如若转载,请注明出处:https://www.28x29.com/archives/9875.html