循环算法Java实现

编程入门 行业动态 更新时间:2024-10-05 17:21:31
本文介绍了循环算法Java实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我认为我的问题很简单,但是我觉得我需要一些不同的观点,因为我似乎无法将此算法转换为代码。

My issue is fairly simple, I think, but I feel that I need some different perspectives, because I cannot seem to translate this algorithm into code.

我需要制定运动队时间表,其中n支球队(在这种情况下为10支球队)以循环赛的形式进行比赛。这些规则遵循基本的循环赛格式,即在给定的时间一个团队只能与另一支球队比赛,所有团队必须与所有其他球队比赛一次。

I need to make a sports team schedule, where n teams (in this case, 10 teams) play in a round robin format. The rules follow basic round-robin formats, where one team can only play one other team at a given time, and all teams must play all other teams once.

我有发现算法是将第1队留在现场,然后将其余的队顺时针旋转。一个虚拟团队可以用来处理n的奇数。该问题出现在算法的顺时针部分。我不知道如何将顺时针旋转的概念转换为我的团队。例如,如果我让他们将其拆分为数组TeamArray,并且TeamArray [0]播放TeamArray [10],依此类推,那么在第1周,如何使它们在第2周顺时针移动?

I have found that the algorithm is to hold team 1 in the spot, and rotate the rest clockwise. A dummy team can be used to handle odd numbers for n. The issue arises in the "clockwise" part of the algorithm. I have no idea how I can translate the concept of rotate clockwise to my teams. For example, if I have them split it in an array TeamArray, and TeamArray[0] plays TeamArray[10], etc, in week 1, how can I make them move clockwise for week 2?

我不是在寻找讲义的答案,而是希望以创造性的方式看待这种算法,以便我可以翻译出移动团队的概念顺时针。

I'm not looking for a handout answer, but rather for some help to look at this algorithm in a creative way so that I can translate the concept of moving the teams clockwise.

我很感谢所有帮助,很乐意回答任何可能困扰我最初问题的问题。谢谢!

I appreciate all the help and would be glad to answer anything that might be confusing in my initial question. Thanks!

推荐答案

一种方法如下:

为球队编号1..n。 (在此示例中,n = 8)在两行中写入所有球队。

Number the teams 1..n. (n=8 in this example) Write all the teams in two rows.

这些列显示了该轮比赛中哪些球队(1比8, 2 vs 7,...)。

The columns show which teams will play in that round (1 vs 8, 2 vs 7, ...).

1 2 3 4 8 7 6 5

现在,保持1个不变,但是(顺时针)旋转所有其他团队。在第2周中,您会得到

Now, keep 1 fixed, but rotate (clockwise) all the other teams. In week 2, you get

1 8 2 3 7 6 5 4

,在第3周,您会得到

1 7 8 2 6 5 4 3

这将持续到第n-1周,在这种情况下,

This continues through week n-1, in this case,

1 3 4 5 2 8 7 6

如果n为奇数,则执行相同的操作,但添加一个虚拟团队。凡是与假人队比赛的人都会在那周再见。

If n is odd, do the same thing but add a dummy team. Whoever is matched against the dummy team gets a bye that week.

例如:

1 2 3 4 5 9 8 7 6 0 (0 being the bye)

继续如上所述的旋转。

代码示例:

void ListMatches(List<string> ListTeam) { if (ListTeam.Count % 2 != 0) { ListTeam.Add("Bye"); // If odd number of teams add a dummy } int numDays = (numTeams - 1); // Days needed to complete tournament int halfSize = numTeams / 2; List<string> teams = new List<string>(); teams.AddRange(ListTeam); // Add teams to List and remove the first team teams.RemoveAt=(0); int teamsSize = teams.Count; for (int day = 0; day < numDays; day++) { Console.WriteLine("Day {0}", (day + 1)); int teamIdx = day % teamsSize; Console.WriteLine("{0} vs {1}", teams[teamIdx], ListTeam[0]); for (int idx = 1; idx < halfSize; idx++) { int firstTeam = (day + idx) % teamsSize; int secondTeam = (day + teamsSize - idx) % teamsSize; Console.WriteLine("{0} vs {1}", teams[firstTeam], teams[secondTeam]); } } }

基本上,这是在做什么将除第一支队伍以外的所有队伍都放在列表中。接下来,将每天开始的索引增加1。对于您关注的这个团队,您可以将该团队与Team1匹配。对于列表中的下一个团队,您可以将其与从列表的另一端开始的相同索引进行匹配,但在第一个索引点为+ 1的任何日期对其进行处理。

Basically what this is doing is putting all teams except the first in a list. Next, increase the index you are starting at by 1 each day. For this team you are focused on, you match this team with Team1. For the next team in the list, you match it to the same index starting from the other end of the list, but treating the first index spot at whatever day it is + 1.

更多推荐

循环算法Java实现

本文发布于:2023-11-29 02:15:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644895.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!