求助!只过了第一个点!

P5740 【深基7.例9】最厉害的学生

___wa___ @ 2022-08-11 19:31:21

代码有点很长
#include<bits/stdc++.h>
using namespace std;
struct xuesheng
{
    string mingzi;
    int yuwen,shuxue,yingyu;
    int zongchengji;
    int id;
}xuesheng[1001];
int main()
{
    int a;
    cin>>a;
    for(int i=0;i<a;i++)
    {
        cin>>xuesheng[i].mingzi;
        cin>>xuesheng[i].yuwen;
        cin>>xuesheng[i].shuxue;
        cin>>xuesheng[i].yingyu;
        xuesheng[i].id=i;
    }
    for(int i=0;i<a;i++)
    {
        xuesheng[i].zongchengji=xuesheng[i].yuwen+xuesheng[i].shuxue+xuesheng[i].yingyu;
    }
    for(int i=0;i<a;i++)
    {
        for(int ii=0;ii<a;ii++)
        {
            if(xuesheng[i].zongchengji<xuesheng[ii].zongchengji&&xuesheng[i].id<xuesheng[ii].id)
            {
                swap(xuesheng[i].zongchengji,xuesheng[ii].zongchengji);
                swap(xuesheng[i].mingzi,xuesheng[ii].mingzi);
                swap(xuesheng[i].id,xuesheng[ii].id);
                swap(xuesheng[i].yuwen,xuesheng[ii].yuwen);
                swap(xuesheng[i].shuxue,xuesheng[ii].shuxue);
                swap(xuesheng[i].yingyu,xuesheng[ii].yingyu);
            }
            else
            if(xuesheng[i].zongchengji==xuesheng[ii].zongchengji)
                if(xuesheng[i].id<xuesheng[ii].id)
                {
                    swap(xuesheng[i].zongchengji,xuesheng[ii].zongchengji);
                    swap(xuesheng[i].mingzi,xuesheng[ii].mingzi);
                    swap(xuesheng[i].id,xuesheng[ii].id);
                    swap(xuesheng[i].yuwen,xuesheng[ii].yuwen);
                    swap(xuesheng[i].shuxue,xuesheng[ii].shuxue);
                    swap(xuesheng[i].yingyu,xuesheng[ii].yingyu);
                }
        }
    }
    cout<<xuesheng[0].mingzi<<" "<<xuesheng[0].yuwen<<" "<<xuesheng[0].shuxue<<" "<<xuesheng[0].yingyu;
    return 0;
 }

样例过了

求dalao帮助!


by wjh213 @ 2022-08-11 19:37:09

似乎是排序有问题


by wjh213 @ 2022-08-11 19:39:15

@wa

if(xuesheng[i].zongchengji<xuesheng[ii].zongchengji&&xuesheng[i].id<xuesheng[ii].id)

你这句代码中的&&xuesheng[i].id<xuesheng[ii].id 有什么用?


by wjh213 @ 2022-08-11 19:40:24

swap似乎可以直接换结构体数组中的两个结构体而不用分开来写


by GuangyuHuashi @ 2022-08-11 19:58:47

排序出错了


by GuangyuHuashi @ 2022-08-11 19:59:41

排序中第一个判定中成绩小就可以直接交换了


by GuangyuHuashi @ 2022-08-11 20:01:05

else中判定也有错误


by GuangyuHuashi @ 2022-08-11 20:01:42

或者说可以去掉


by hyc12528 @ 2022-08-11 20:02:31

这不用排序吧……


by GuangyuHuashi @ 2022-08-11 20:06:04

AC:

#include<bits/stdc++.h>
using namespace std;
struct xuesheng
{
    string mingzi;
    int yuwen,shuxue,yingyu;
    int zongchengji;
    int id;
}xuesheng[1001];
int main()
{
    int a;
    cin>>a;
    for(int i=0;i<a;i++)
    {
        cin>>xuesheng[i].mingzi;
        cin>>xuesheng[i].yuwen;
        cin>>xuesheng[i].shuxue;
        cin>>xuesheng[i].yingyu;
        xuesheng[i].id=i;
    }
    for(int i=0;i<a;i++)
    {
        xuesheng[i].zongchengji=xuesheng[i].yuwen+xuesheng[i].shuxue+xuesheng[i].yingyu;
    }
    for(int i=0;i<a-1;i++)
    {
        for(int ii=i+1;ii<a;ii++)
        {
            if(xuesheng[i].zongchengji<xuesheng[ii].zongchengji)
            {
                swap(xuesheng[i].zongchengji,xuesheng[ii].zongchengji);
                swap(xuesheng[i].mingzi,xuesheng[ii].mingzi);
                swap(xuesheng[i].id,xuesheng[ii].id);
                swap(xuesheng[i].yuwen,xuesheng[ii].yuwen);
                swap(xuesheng[i].shuxue,xuesheng[ii].shuxue);
                swap(xuesheng[i].yingyu,xuesheng[ii].yingyu);
            }
        }
    }
    cout<<xuesheng[0].mingzi<<" "<<xuesheng[0].yuwen<<" "<<xuesheng[0].shuxue<<" "<<xuesheng[0].yingyu;
    return 0;
 }

by GuangyuHuashi @ 2022-08-11 20:07:01

@hyc12528 是不用,但尽量按楼主的思路来(个人想法)


| 下一页