PTA |1015 德才论 分数 25 枚举

125 阅读1分钟

PTA | 程序设计类实验辅助教学平台 (pintia.cn)

其实就是把题目给的所有情况给枚举出来

尽量简写,刚开始写的,冗余还长还报错:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n, l, h;
    cin >> n >> l >> h;

    vector<Student> students;
    for (int i = 0; i < n; i++) {
        Student student;
        cin >> student.xh >> student.Dcj >> student.Ccj;
        students.push_back(student);
    }

    vector<Student> category1, category2, category3, category4, category5;
    for (const Student& student : students) {
        if (student.Dcj >= h && student.Ccj >= h) {
            category1.push_back(student);
        } else if (student.Dcj >= h && student.Ccj < l) {
            category2.push_back(student);
        } else if (student.Ccj < h && student.Dcj < h && student.Dcj > student.Ccj) {
            category3.push_back(student);
        } else if (student.Dcj >= l && student.Ccj >= l) {
            category4.push_back(student);
        } else {
            category5.push_back(student);
        }
    }

    sort(category1.begin(), category1.end(), compareByScore);
    sort(category2.begin(), category2.end(), compareByScore);
    sort(category3.begin(), category3.end(), compareByScore);
    sort(category4.begin(), category4.end(), compareByScore);
    sort(category5.begin(), category5.end(), compareByScore);

    cout << category1.size() + category2.size() + category3.size() + category4.size() + category5.size() << endl;
    for (const Student& student : category1) {
        cout << student.xh << " " << student.Dcj << " " << student.Ccj << endl;
    }
    for (const Student& student : category2) {
        cout << student.xh << " " << student.Dcj << " " << student.Ccj << endl;
    }
    for (const Student& student : category3) {
        cout << student.xh << " " << student.Dcj << " " << student.Ccj << endl;
    }
    for (const Student& student : category4) {
        cout << student.xh << " " << student.Dcj << " " << student.Ccj << endl;
    }
    for (const Student& student : category5) {
        cout << student.xh << " " << student.Dcj << " " << student.Ccj << endl;
    }

    return 0;
}

AC代码

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<stdio.h>
using namespace std;
struct student
{
    int xh;
    int de;
    int cai;
    int sum;
};
bool cmp(student a,student b)
{
    //如果总成绩相同就按总成绩大的排序
    if(a.sum!=b.sum)return a.sum>b.sum;
    else 
    {
        //否则总成绩不相同,德成绩也不相同 就按德成绩大的排
        if(a.de!=b.de) return a.de>b.de;
    }
    //总成绩相同 并且 德成绩也相同就按学号升序排列
    return a.xh<b.xh;
}
int main()
{
    vector <student>v[4]; 
    
    int n,l,h,total=0;
    
    scanf("%d %d %d",&n,&l,&h);
    student  s;
    
    for(int i=0;i<n;i++)
    {
        scanf("%d %d %d",&s.xh,&s.de,&s.cai);
        
        s.sum=s.de+s.cai;
        if(s.de>=l&&s.cai>=l)
        {
            total++;
            //德才兼备
            if(s.de>=h&&s.cai>=h)
            {
                v[0].push_back(s);
            }
            //德胜才
            else if(s.de>=h&&s.cai<h)
            {
                v[1].push_back(s);
            }
            //才德兼亡,但德胜才
            else if(s.de<h&&s.de>=s.cai)
            {
                v[2].push_back(s);
            }
            //及格
            else
            {
                v[3].push_back(s);
            }
        }
    }
    printf("%d\n",total);
    for(int i=0;i<4;i++)
    {
        sort(v[i].begin(),v[i].end(),cmp);

        
        for(int j=0;j<v[i].size();j++)
        {
            printf("%d %d %d\n",v[i][j].xh,v[i][j].de,v[i][j].cai);
        }
    }
}