炸裂,80分

P1104 生日

Miracle_InDream @ 2023-12-02 20:21:31

#include<bits/stdc++.h>
using namespace std;
struct student
{
    string name;
    long long year;
    long long month;
    long long day; 
    bool flag=1;
};
int main()
{
    int n;
    cin>>n;
    student students[105];
    for(int i=0;i<n;i++)
    {
        cin>>students[i].name>>students[i].year>>students[i].month>>students[i].day;
    }
    for(int j=0;j<n;j++)
    {
        for(int i=0;i<n-1;i++)
        {
            if(students[i].year>students[i+1].year)
            {
                swap(students[i],students[i+1]);
            }
            else if(students[i].year==students[i+1].year&&students[i].month>students[i+1].month)
            {
                swap(students[i],students[i+1]);
            }
            else if(students[i].year==students[i+1].year&&students[i].month==students[i+1].month&&students[i].day>students[i+1].day)
            {
                swap(students[i],students[i+1]);
            }
            else if((students[i].year==students[i+1].year&&students[i].month==students[i+1].month&&students[i].day==students[i+1].day)&&(students[i].flag||students[i+1].flag))
            {
                swap(students[i],students[i+1]);
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        cout<<students[i].name<<endl;
    }
    return 0;
}

本来准备自己下载测试点检查的,但是测试完后对照的过程中眼睛都花了……


by _buzhidao_ @ 2023-12-02 20:24:56

@shooting__star

#include<bits/stdc++.h>
using namespace std;
struct stu{
    string name;int y,m,d,id;
} s[105];
int n;
bool cmp(stu a,stu b){
    if(a.y==b.y){
        if(a.m==b.m){
            if(a.d==b.d){
                return a.id>b.id;
            }
            return a.d<b.d;
        }
        return a.m<b.m;
    }
    return a.y<b.y;
}
int main(){
    cin>>n;for(int i=0;i<n;++i) cin>>s[i].name>>s[i].y>>s[i].m>>s[i].d,s[i].id=i;
    sort(s,s+n,cmp);
    for(int i=0;i<n;++i) cout<<s[i].name<<endl;
    return 0;
}

by _buzhidao_ @ 2023-12-02 20:25:48

@shooting__star 尽量不要手打排序,直接sort函数搞定


by xxgirlxx @ 2023-12-02 20:53:51

@shooting__star 你说你何苦呢?非得手打排序?这题的数据范围sort又不会超时。

#include<bits/stdc++.h>
using namespace std;
int n;
struct v{
    int nian,yue,ri,id;
    string ming;
}a[1000000];
bool cmp(v x,v y){
    if(x.nian==y.nian&&x.yue==y.yue&&x.ri==y.ri)return x.id>y.id;
    if(x.nian==y.nian&&x.yue==y.yue)return x.ri<y.ri;
    if(x.nian==y.nian)return x.yue<y.yue;
    return x.nian<y.nian;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].ming>>a[i].nian>>a[i].yue>>a[i].ri;
        a[i].id=i;
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++)cout<<a[i].ming<<endl;
    return 0;
}

|