• 【PAT A1025】部分正确->全部正确


    #include <algorithm>
    #include <stdio.h>
    #include <string.h>
    #include <fstream>
    #include <iostream>
    #include <string.h>
    #include <cstring>
    #include <vector>
    using namespace std;
    #define maxN 105
    #define manK 305
    #define manS 30010
    
    struct Stu {
        char regist[16];
        int socre;
        int finalRank;
        int locationNumber;
        int localRank;
        Stu() {}
        Stu(char _regist[], int _socre, int _locationNumber) {
            strcpy(this->regist,_regist);
            this->socre =_socre;
            this->locationNumber = _locationNumber;
        }
    }static students[manS];
    
    static int N;
    static int K;
    static int numS;
    
    bool cmp(Stu stu1, Stu stu2) {
        if(stu1.socre != stu2.socre) return stu1.socre > stu2.socre;
        else return strcmp(stu1.regist,stu2.regist) < 0;
    }
    
    void groupSort(int K) {
        sort(students + (numS-K), students + numS, cmp);
        Stu *stu = students + (numS-K);
        stu->localRank = 1;
        for (int i = 1; i < K; i++) {
            Stu* preStu = students + (numS-K+i-1);
            Stu* stu = students + (numS-K+i);
            if(stu->socre == preStu->socre) stu->localRank = preStu->localRank;
            else stu->localRank = i+1;
        }
        return;
    }
    
    void fileInput() {
        ifstream fin;
        fin.open("/home/zzz/input.txt",ios::in);
        fin >> N;
        numS = 0;
        for (int i = 0; i < N; i++) {
            fin >> K;
            for (int j = 0; j < K; j++) {
                char _regist[16];
                int _score;
                fin >> _regist >> _score;
                students[numS] = Stu(_regist, _score, i+1);
                numS++;
            }
            groupSort(K);
        }
        return;
    }
    
    void stdInput() {
        cin >> N;
        numS = 0;
        for (int i = 0; i < N; i++) {
            cin >> K;
            for (int j = 0; j < K; j++) {
                char _regist[16];
                int _score;
                cin >> _regist >> _score;
                students[numS] = Stu(_regist, _score, i + 1);
                numS++;
            }
            groupSort(K);
        }
        return;
    }
    
    int main() {
        stdInput();
        cout << numS << endl;
        sort(students, students + numS, cmp);
        Stu* stu = students;
        stu->finalRank = 1;
        printf("%s %d %d %d
    ", stu->regist, stu->finalRank, stu->locationNumber, stu->localRank);
        for (int i = 1; i < numS; i++) {
            Stu* stu = students + i;
            Stu* preStu = students + i - 1;
            if(stu->socre == preStu->socre) stu->finalRank = preStu->finalRank;
            else stu->finalRank = i+1;
            printf("%s %d %d %d
    ", stu->regist, stu->finalRank, stu->locationNumber, stu->localRank);
        }
        return 0;
    }
    

    我把manS放到30005部分正确,把它开到30010即全部正确、、、见了贵了

    刷题感悟:

    • 全局变量尽量加上static关键词
    • 结构体数组尽量使用指针
    • 渐渐往高级题目走的时候,不要仍然局限在模拟思维——怎么说,怎么做,学会开动脑筋多加思考,更灵活地解决问题
    • “分而治之”的思想
      • 分组排序是全体排序的子问题
      • 说白了就是一个一模一样的问题,就是处理问题的“域”不一样
  • 相关阅读:
    pdf文件怎么转换成word文档
    数据库的发展历程
    数据库的三级模式
    数据库概述
    时间序列的自回归模型—从线性代数的角度来看
    数据清洗
    sql commit的三种方式
    数据库标准语言SQL((Structured Query Language)
    正则化推导转载
    leetcode刷题
  • 原文地址:https://www.cnblogs.com/huangming-zzz/p/11673487.html
Copyright © 2020-2023  润新知