PAT 乙级 1015 题解 (Basic Level) Practice

一开始用C++的STL超时,又换成了C语言

#include
#include
struct stu
{
    int num;
    int de;
    int cai;
};

int comp(const void *ss1,const void *ss2)
{

    const struct stu * s1 = (const struct stu *)ss1;
    const struct stu * s2 = (const struct stu *)ss2;
    int zong1 = s1->de+s1->cai;
    int zong2 = s2->de+s2->cai;
    if( zong1!= zong2)
        return zong1de!=s2->de)
        return s1->dede;
    return s1->num>s2->num;
}
int main()
{
    int N,L,H;
    scanf("%d %d %d",&N,&L,&H);
    struct stu v[N];

    int i,j;

    for(i=0,j=0;i=H && v[i].cai>=H)
        {
            v1[j1++]=v[i];
            continue;
        }
        if(v[i].de>=H)
        {
            v2[j2++]=v[i];
            continue;
        }
        if(v[i].de>= v[i].cai)
        {
            v3[j3++]=v[i];
            continue;
        }
        v4[j4++]=v[i];
    }
    qsort(v1,j1,sizeof(struct stu),comp);
    qsort(v2,j2,sizeof(struct stu),comp);
    qsort(v3,j3,sizeof(struct stu),comp);
    qsort(v4,j4,sizeof(struct stu),comp);
    printf("%d\n",j);

    for(i=0;i

下面是超时的代码:

#include
#include
#include
using namespace std;
struct stu
{
    int num;
    int de;
    int cai;
};

bool operator<(const stu &s1,const stu &s2)
{
    int zong1 = s1.de+s1.cai;
    int zong2 = s2.de+s2.cai;
    if( zong1!= zong2)
        return zong1>zong2;
    if(s1.de!=s2.de)
        return s1.de>s2.de;
    return s1.num>N>>L>>H;
    vector v;

    int i;

    for(i=0;i>s.num>>s.de>>s.cai;
        if(s.de v1;
    vector v2;
    vector v3;
    vector v4;
    for(i=0;i=H && v[i].cai>=H)
        {
            v1.push_back(v[i]);
            continue;
        }
        if(v[i].de>=H)
        {
            v2.push_back(v[i]);
            continue;
        }
        if(v[i].de>= v[i].cai)
        {
            v3.push_back(v[i]);
            continue;
        }
        v4.push_back(v[i]);
    }
    sort(v1.begin(),v1.end());
    sort(v2.begin(),v2.end());
    sort(v3.begin(),v3.end());
    sort(v4.begin(),v4.end());
    cout<

 

 

你可能感兴趣的:(PAT乙级)