#39037: C++ 用struct, sort解


sammy60406test@gmail.com (MUSEr)

學校 : 不指定學校
編號 : 255059
來源 : [101.137.100.54]
最後登入時間 :
2024-04-14 10:57:12
m931. 1. 遊戲選角 -- 2024年1月APCS | From: [101.137.215.100] | 發表日期 : 2024-01-08 23:06

用struct創一個陣列,裡面分別存攻擊力跟防禦力

然後用一個自定義比較

 

struct S{

int d, f;

bool operator<(S b)

{

return d*d + f*f < b.d*b.d + b.f*b.f;

}

};

S people[100];

 
#39038: Re: C++ 用struct, sort解


sammy60406test@gmail.com (MUSEr)

學校 : 不指定學校
編號 : 255059
來源 : [101.137.100.54]
最後登入時間 :
2024-04-14 10:57:12
m931. 1. 遊戲選角 -- 2024年1月APCS | From: [101.137.215.100] | 發表日期 : 2024-01-08 23:08

用struct創一個陣列,裡面分別存攻擊力跟防禦力

然後用一個自定義比較

 

struct S{

int d, f;

bool operator<(S b)

{

return d*d + f*f < b.d*b.d + b.f*b.f;

}

};

S people[100];

sort完輸出n-2項就ok

 
ZeroJudge Forum