auspicious2013 @ 2024-08-27 19:24:35
如题,c++代码如下:
#include<bits/stdc++.h>
using namespace std;
int n;
struct birth{
string s;
int y,m,d,k;
}a[105];
bool cmp(birth q,birth p){
if(q.y!=p.y){
return q.y<p.y;
}else{
if(q.m!=p.m){
return q.m<p.m;
}else{
if(q.d!=p.d){
return q.d<p.d;
}else{
return q.k<p.k;
}
}
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].s>>a[i].y>>a[i].m>>a[i].d;
a[i].k=i;
}
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
cout<<a[i].s<<endl;
}
return 0;
}
by _O__o_ @ 2024-08-27 19:36:51
n 个生日从大到小同学的姓名。(如果有两个同学生日相同,输入靠后的同学先输出)
by kanqiqin @ 2024-08-27 19:37:00
#include<bits/stdc++.h>
using namespace std;
int n;
struct tx{
string name;
int year,month,day,No;
};
int cmp(tx a,tx b)
{
if(a.year!=b.year)return a.year>b.year;
if(a.month!=b.month)return a.month>b.month;
if(a.day!=b.day)return a.day>b.day;
return a.No<b.No;
}
int main()
{
cin>>n;
tx s[102];
for(int i=0;i<n;i++)
{
cin>>s[i].name>>s[i].year>>s[i].month>>s[i].day;
s[i].No=i;
}
stable_sort(s,s+n,cmp);
for(int i=n-1;i>=0;i--)
cout<<s[i].name<<endl;
return 0;
}
by _O__o_ @ 2024-08-27 19:37:54
@auspicious2013
#include<bits/stdc++.h>
using namespace std;
int n;
struct birth{
string s;
int y,m,d,k;
}a[105];
bool cmp(birth q,birth p){
if(q.y!=p.y){
return q.y<p.y;
}else{
if(q.m!=p.m){
return q.m<p.m;
}else{
if(q.d!=p.d){
return q.d<p.d;
}else{
return q.k>p.k;
}
}
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].s>>a[i].y>>a[i].m>>a[i].d;
a[i].k=i;
}
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
cout<<a[i].s<<endl;
}
return 0;
}
更改了第18行,将
return q.k<p.k
改为
return q.k>p.k
by _O__o_ @ 2024-08-27 19:38:58
@kanqiqin 哇又是你
by kanqiqin @ 2024-08-27 19:40:00
咳咳
你好
互关一下
by kanqiqin @ 2024-08-27 19:40:33
@_O_o
by _O__o_ @ 2024-08-27 19:41:42
@kanqiqin OK了
by jubaoyi2011 @ 2024-08-27 19:43:47
#include<bits/stdc++.h>
using namespace std;
int n;
struct node{
string name;
int year,month,day,num;
}a[110];
bool cmp(node a,node b){
if(a.year<b.year)return true;
if(a.year>b.year)return false;
if(a.year==b.year){
if(a.month<b.month)return true;
if(a.month>b.month)return false;
if(a.month==b.month){
if(a.day<b.day)return true;
if(a.day>b.day)return false;
if(a.day==b.day){
if(a.num>b.num)return true;
else return false;
}
}
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].name>>a[i].year>>a[i].month>>a[i].day;
a[i].num=i;
}
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
cout<<a[i].name<<endl;
}
return 0;
}
by jubaoyi2011 @ 2024-08-27 19:44:21
@auspicious2013
by auspicious2013 @ 2024-08-28 12:45:59
@jubaoyi2011 @_O_o @kanqiqin thanks,已关