分析和订购足球联赛球队 c++

Analyse and order football league teams c++

本文关键字:联赛 c++      更新时间:2023-10-16

>我正在尝试以非常特定的顺序显示足球联赛获胜者的最佳解决方案。问题如下:

您输入参加比赛的球队数量。然后你以矩阵形式输入所有球队的分数( mi,j = (x,y) 表示球队 i 进了 x 球,球队 j 进了 y)。

所需的输出将是包含以下信息的团队排名列表:团队编号、团队积分、完成的团队目标、收到的团队目标。首先是积分更多的球队,如果两支球队的积分相同,第一个将是净胜球最好的球队(完成 - 收到),如果相同,则顺序只是球队的数量。如果你赢了,你会得到3分,如果你平局,你会得到1分。

Sample input
4
0 0   1 0   2 1   0 2
2 2   0 0   3 3   1 3
1 1   1 2   0 0   3 2
1 0   0 1   2 3   0 0 
Sample output
4 9 10 8
3 8 12 12
1 8 6 7
2 8 9 10

这是一个比我习惯处理的问题更复杂的问题(这很棒)。我遇到的问题是我无法决定如何处理订购系统。我认为最好的办法是将积分、完成的进球和收到的进球保存在另一个矩阵中,但我不知道我将如何排序。为了分析分数,我想我会使用不同的功能做一个平局/赢/输工作流程,以了解我必须保存哪些点,首先垂直穿过矩阵(跳过主对角线),然后水平。 我应该如何处理排序系统以显示排名表?另一个矩阵是存储点、目标的最佳解决方案吗?

这是我目前设法完成的简单代码:

#include <iostream>
#include <vector>
#include <utility>
using namespace std;

bool draw(const vector< vector<pair<int,int>> > &scores, int x, int y)  { // Is it a draw?
    if (scores[x][y].first == scores[x][y].second) return true;
    return false;
}

bool x_win(const vector< vector<pair<int,int>> > &scores, int x, int y) { // Is it a win for team x?
    if (scores[x][y].first > scores[x][y].second) return true;
    return false;
}

void input_pairs_in_matrix(vector< vector<pair<int,int>> > &scores, int n) { // input pairs
    int n1,n2;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin>>n1>>n2;
            pair<int,int> p = make_pair(n1,n2);
            scores[i][j] = p;
        }
    }
}

int main (){
    int n; cin >> n; // number of teams

    vector< vector<pair<int,int>> > scores(n,vector<pair<int,int>>(n)); //matrix of pairs

    input_pairs_in_matrix(scores,n);
}

PD:我不是在寻找完整的解决方案,因为这是家庭作业,但我很迷茫,希望得到一些提示/建议。

在C++编码时,您应该尝试使用class。它们确实有助于将您的问题分解成更易于理解、测试和使用的小块。

对于您的问题,我将创建一个班级团队:

class Team
{
    public:
        unsigned int points;
        unsigned int goals_marked;
        unsigned int goals_received;
}

我把所有东西都公开为一个最小的答案,你可能想要一个更完整的类,也许有operator>>来解码它,等等......然后,您可以在此类型上创建一个operator<,以帮助您进行排序:

bool operator<(Team &lh, Team &rh)
{
    // return true if lh is less good than rh
}

那么排序只是在向量上调用sort的问题:

std::vector<Team> teams;
// Read your class and fill teams
std::sort(teams.begin(), teams.end());