- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
(编辑代码)我遇到了一些问题,希望能得到一些帮助。这是我的条件:
You are developing a program to keep track of team standings in a league. When a game is played, the winning team (the team with the higher score) gets 2 points and the losing team gets no points. If there is a tie, both teams get 1 point. The order of the standings must be adjusted whenever the results of a game between two teams are reported. The following class records the results of one game.
public class GameResult
{
public String homeTeam() // name of home team
{ /* code not shown */ }
public String awayTeam() // name of away team
{ /* code not shown */ }
public int homeScore() // score for home team
{ /* code not shown */ }
public int awayScore() // score for away team
{ /* code not shown */ }
// instance variables, constructors, and other methods not shown
}
The information for each team is stored by an instance of the class TeamInfo whose partial definition is below.
public class TeamInfo
{
public String teamName()
{ /* code not shown */ }
public void increasePoints(int points)
{ /* code not shown */ }
public int points()
{ /* code not shown */ }
// instance variables, constructors, and other methods not shown
}
The class TeamStandings stores information on the team standings. A partial declaration is shown below.
public class TeamStandings
{
TeamInfo[] standings; // maintained in decreasing order by points,
// teams with equal points can be in any order
public void recordGameResult(GameResult result)
{ /* to be completed as part (c) */ }
private int teamIndex(String name)
{ /* to be completed as part (a) */ }
private void adjust(int index, int points)
{ /* to be completed as part (B)/> */ }
// constructors and other methods not shown
}
这是实际的问题:
Write the method adjust. The method adjust should increment the team points for the team found at the index position in standings by the amount given by the parameter points. In addition, the position of the team found at index in standings should be changed to maintain standings in decreasing order by points; teams for which points are equal can appear in any order.
这是我到目前为止所得到的:
private void adjust(int index, int points)
{
int Score[] = new int[standings.length]
for ( int i=0; i < standings.length; i++)
{
Score[i] = points;
Arrays.sort(Score);
}
}
我意识到这是非常错误的,需要一些指导来解决这个问题。谢谢!
最佳答案
这样的事情应该有效:
private void adjust(int index, int points) {
// increase points of winning team
TeamInfo curr = standings[index];
curr.increasePoints(points);
// get the new score of the winning team
int points = curr.points();
// perform an insertion sort on the modified portion
int i = index;
while (i > 0 && standings[i-1].points() < points) {
// shift teams with lower scores rightwards
standings[i] = standings[i-1];
i--;
}
standings[i] = curr;
}
基本上,它只是在指定的 index
参数处获取获胜团队 (curr
) 并增加其分数。由于列表必须按照球队积分降序排列,因此调整积分后将球队插入到正确的位置即可。
关于java - 计算机科学常见问题解答,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23155966/
我是一名优秀的程序员,十分优秀!