標題:
排序 - struct
[打印本頁]
作者:
tonyh
時間:
2022-12-22 19:39
標題:
排序 - struct
本帖最後由 tonyh 於 2022-12-22 20:23 編輯
試自訂比較方法,對學生們的成績做遞減排序。
原始資料:
Bob 70 分
Cindy 66 分
Alice 77 分
Mary 76 分
排序後:
Alice 77 分
Mary 76 分
Bob 70 分
Cindy 66 分
#include<bits/stdc++.h>
using namespace std;
struct student
{
string name;
int score;
};
bool compare(student a, student b)
{
return a.score>b.score;
}
student st[4];
int main()
{
st[0].name="Bob";
st[1].name="Cindy";
st[2].name="Alice";
st[3].name="Mary";
st[0].score=70;
st[1].score=66;
st[2].score=77;
st[3].score=76;
for(student s: st)
cout<<s.name<<"\t"<<s.score<<" 分"<<endl;
cout<<"-------------"<<endl;
sort(st, st+4, compare);
for(student s: st)
cout<<s.name<<"\t"<<s.score<<" 分"<<endl;
cout<<"-------------"<<endl;
return 0;
}
複製代碼
作者:
黃宇綸
時間:
2022-12-22 20:23
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,(n)+1)
#define RREP(i,n) for(int i=(n)-1;i>=0;i--)
#define f first
#define s second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define SQ(x) (x)*(x)
#define pii pair<int,int>
#define Graph vector<vector<int>>
#define IOS() cin.sync_with_stdio(0),cin.tie(0),cout.tie(0)
const ll inf=(1ll<<63)-1;
const int maxn=1e5+5;
const ll mod=1e9+7;
struct P {
string na;
int sc;
};
vector<P> v;
bool operator<(P a,P b) {
return a.sc<b.sc;
}
signed main()
{
IOS();
v.pb({"Bob",70});
v.pb({"Cindy",66});
v.pb({"Alice",77});
v.pb({"Mary",76});
for(auto p:v) cout<<p.na<<" "<<p.sc<<" 分\n"; cout<<"\n";
sort(ALL(v));
cout<<"排序後:\n";
for(auto p:v) cout<<p.na<<" "<<p.sc<<" 分\n"; cout<<"\n";
return 0;
}
複製代碼
作者:
李沛昂
時間:
2022-12-22 20:25
#include<bits/stdc++.h>
using namespace std;
struct student
{
string name;
int score;
};
bool compare(student a, student b)
{
return a.score>b.score;
}
student st[4];
int main()
{
st[0].name="Bob";
st[1].name="Cindy";
st[2].name="Alice";
st[3].name="Mary";
st[0].score=70;
st[1].score=66;
st[2].score=77;
st[3].score=76;
for(student s: st)
cout<<s.name<<"\t"<<s.score<<" 分"<<endl;
cout<<"-------------"<<endl;
sort(st, st+4, compare);
for(student s: st)
cout<<s.name<<"\t"<<s.score<<" 分"<<endl;
cout<<"-------------"<<endl;
return 0;
}
複製代碼
作者:
黃宥華
時間:
2022-12-22 20:26
#include<bits/stdc++.h>
using namespace std;
struct student{
string name;
int score;
};
bool compare(student a,student b){
return a.score>b.score;
}
student st[4];
void show(){
for(student s:st){
cout<<s.name<<" "<<s.score<<"分"<<endl;
}
cout<<endl;
}
int main()
{
cin.tie(0);
cin.sync_with_stdio(0);
st[0].name="Bob";
st[0].score=70;
st[1].name="Cindy";
st[1].score=66;
st[2].name="Alice";
st[2].score=77;
st[3].name="Mary";
st[3].score=76;
show();
sort(st,st+4,compare);
show();
return 0;
}
複製代碼
作者:
黃宇瑄
時間:
2022-12-22 20:27
#include<bits/stdc++.h>
using namespace std;
struct student
{
string name;
int score;
};
bool compare(student a, student b)
{
return a.score>b.score;
}
student st[4];
int main()
{
st[0].name="Bob";
st[1].name="Cindy";
st[2].name="Alice";
st[3].name="Mary";
st[0].score=70;
st[1].score=66;
st[2].score=77;
st[3].score=76;
for(student s: st)
cout<<s.name<<"\t"<<s.score<<" 分"<<endl;
sort(st, st+4, compare);
for(student s: st)
cout<<s.name<<"\t"<<s.score<<" 分"<<endl;
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2