Dpf20120621 @ 2024-02-25 16:00:05
#include<bits/stdc++.h>
using namespace std;
struct node{
string s;
int y;
int m;
int d;
int id;
}x[101];
string w;
int h,q,p;
bool cmp(node a,node b){
if(a.y==b.y){
if(a.m==b.m){
if(a.d==b.d)return a.id<b.id;
else return a.d<b.d;
}else return a.m<b.m;
}else return a.y<b.y;
}
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
cin>>w>>h>>q>>p;
x[i].s=w;
x[i].y=h;
x[i].id=i;
x[i].m=q;
x[i].d=p;
}
sort(x,x+n,cmp);
for(int i=0;i<n;i++)cout<<x[i].s<<endl;
return 0;
}
by zml1029 @ 2024-07-29 10:47:10
你这题其实只需要用到结构体就行了,条件判断可以在int main()里,而且你的条件判断有一点错误
#include<bits/stdc++.h>
using namespace std;
struct student{
string name;
int y,m,d,id;
};
int main(){
int n;
cin>>n;
student a[105];
for(int i=1;i<=n;i++){
cin>>a[i].name>>a[i].y>>a[i].m>>a[i].d;
a[i].id=i;
}
for(int i=1;i<=n-1;i++){
for(int j=i+1;j<=n;j++){
if(a[i].y>a[j].y||
a[i].y==a[j].y&&a[i].m>a[j].m||
a[i].y==a[j].y&&a[i].m==a[j].m&&a[i].d>a[j].d||
a[i].y==a[j].y&&a[i].m==a[j].m&&a[i].d==a[j].d&&a[i].id<a[j].id){
swap(a[i],a[j]);
}
}
}
for(int i=1;i<=n;i++){
cout<<a[i].name<<endl;
}
return 0;
}