#39037: C++ 用struct, sort解


sammy60406test@gmail.com (MUSEr)

學校 : 不指定學校
編號 : 255059
來源 : [223.23.12.147]
最後登入時間 :
2025-09-30 22:49:43

用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
來源 : [223.23.12.147]
最後登入時間 :
2025-09-30 22:49:43

用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