1 条题解

  • 0
    @ 2025-9-9 23:46:00

    C++ :

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    
    typedef struct tagStudent
    {
    	short num;
    	short marks;
    } STU, *PSTU;
    
    void InitData(PSTU& pStu, const short& nPeople);
    int comp(const STU& stu1, const STU& stu2);
    short CalcLimit(const PSTU& pStu, const short& nPeople, const short& nPlan);
    void DisplayRes(const PSTU& pStu, const short nLimit);
    void DestroyMem(PSTU& pStu);
    
    int main()
    {
    	short nPeople = 0;
    	short nPlan = 0;
    	short nLimit = 0;
    	PSTU pStu = NULL;
    	
    	cin >>nPeople >>nPlan;
    	InitData(pStu, nPeople);
    	sort(pStu, pStu + nPeople, comp);
    	nLimit = CalcLimit(pStu, nPeople, nPlan);
    	DisplayRes(pStu, nLimit);
    	DestroyMem(pStu);
    	return 0;
    }
    
    void InitData(PSTU& pStu, const short& nPeople)
    {
    	pStu = new STU[nPeople];
    	memset(pStu, 0, nPeople*sizeof(STU));
    	
    	// 输入
    	for (int i = 0; i < nPeople; ++i)
    	{
    		cin >>pStu[i].num >>pStu[i].marks;
    	}
    }
    
    short CalcLimit(const PSTU& pStu, const short& nPeople, const short& nPlan)
    {
    	short people = (short)(nPlan * 1.5f);
    	if (people >= nPeople)
    	{
    		return nPeople;
    	}
    
    	short marks = pStu[people-1].marks;
    	short pos = people;
    	while (pos < nPeople && pStu[pos].marks == marks) ++pos;
    	return pos;
    }
    
    void DisplayRes(const PSTU& pStu, const short nLimit)
    {
    	cout <<pStu[nLimit-1].marks <<' ' <<nLimit <<endl;
    	for (int i = 0; i < nLimit; ++i)
    	{
    		cout <<pStu[i].num <<' ' <<pStu[i].marks <<endl;
    	}
    }
    
    void DestroyMem(PSTU& pStu)
    {
    	delete []pStu;
    	pStu = NULL;
    }
    
    int comp(const STU& stu1, const STU& stu2)
    {
    	if (stu1.marks == stu2.marks)
    	{
    		return stu1.num <stu2.num;
    	}
    	return stu1.marks > stu2.marks;
    }
    
    • 1

    信息

    ID
    949
    时间
    1000ms
    内存
    64MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者