Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions md/0028.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
เพื่อหาทีมที่ชนะ เราจะสร้าง Structure ที่เก็บข้อมูลของแต่ละทีม โดยจะเก็บชื่อทีม คะแนน และจำนวนประตูได้ เสียของแต่ละทีม โดยเราให้ $\text{goal}[i][j]$ เก็บประตูที่ทีม $i$ ยิงตอนแข่งกับทีม $j$

เรากำหนดวิธีการเปรียบเทียบสองทีมโดยให้เรียงจากคะแนน แต่ถ้าคะแนนเท่ากันให้ดูแต้มได้เสียตามวิธีที่กำหนดในโจทย์

จากนั้นใช้ฟังก์ชั่น sort ในการเรียงข้อมูลตามวิธีที่กำหนดไว้ และแสดงผลทีมออกมาตามลำดับ

```cpp
#include<bits/stdc++.h>
using namespace std;

struct Team {
string name;
int score, total_goal_scored, total_goal_lost;
bool operator < (const Team & o) const {
int goal_advantage = total_goal_scored - total_goal_lost;
int o_goal_advantage = o.total_goal_scored - o.total_goal_lost;
if (score == o.score && (goal_advantage == o_goal_advantage))
return total_goal_scored < o.total_goal_scored;
if (score == o.score)
return goal_advantage < o_goal_advantage;
return score < o.score;
}
} teams[4];

int main () {
for (int i = 0; i < 4; i++) {
cin >> teams[i].name;
}
int goal[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cin >> goal[i][j];
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i == j) continue;
if (goal[i][j] > goal[j][i]) teams[i].score += 3;
if (goal[i][j] == goal[j][i]) teams[i].score += 1;
teams[i].total_goal_scored += goal[i][j];
teams[i].total_goal_lost += goal[j][i];
}
}
sort(teams, teams+4);
for (int i = 3; i >= 0; i--) {
cout << teams[i].name << ' ' << teams[i].score << "\n";
}
return 0;
}
```