我试着想出一个最好的解决方案,以一个非常具体的顺序展示一个足球联赛的赢家。问题如下:
你输入参赛队伍的数量。然后,以矩阵的形式输入所有球队的分数( 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:我并不是在寻找完整的解决方案,因为这是家庭作业,但是我很迷茫,很想知道一些建议/建议。
发布于 2016-02-24 14:05:48
在用class编写代码时,应该尝试使用C++。它们确实有助于将你的问题分解成更容易理解、测试和使用的小块。
对于您的问题,我将创建一个类团队:
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
}那么排序只是在向量上调用排序的问题:
std::vector<Team> teams;
// Read your class and fill teams
std::sort(teams.begin(), teams.end()); https://stackoverflow.com/questions/35602444
复制相似问题