Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8551627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:14:12+00:00 2026-06-11T14:14:12+00:00

this works for me but I don’t understand how it works at all. Could

  • 0

this works for me but I don’t understand how it works at all. Could anyone explain?

for(int round = 0; round < rounds_count; round++)
{
    for(int match = 0; match < matches_per_round; match++)
    {
        int home = (round + match) % (teams_count - 1);
        int away = (teams_count - 1 - match + round) % (teams_count - 1);

        if(match == 0)
            away = teams_count - 1;

        matches.push_back(Match(&teams[home], &teams[away], round));
    }
}

What’s the trick with modulo?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:14:13+00:00Added an answer on June 11, 2026 at 2:14 pm

    I’m not sure why this would be using teams_count-1 instead of teams_count, but in general, the modulus is making it “wrap around” so that if round+match is greater than the last team number, it will wrap back ground to one of the first teams instead of going past the last team.

    The way away is handled, is a bit special. The % operator doesn’t wrap around the way you want when you have negative numbers. For example -1 % 5 gives you -1 instead of 4. A trick to get around this problem is to add your divisor. (-1+5)%5 gives you 4.

    Let’s rework the code a little to make it clearer. First I’ll use another variable n to represent the number of teams (again I’m not sure why teams_count-1 is used for this in your code):

    int n = teams_count-1;
    int home = (round + match) % n;
    int away = (n - match + round) % n;
    

    Then I’ll reorganize the away calculation a little:

    int n = teams_count-1;
    int home = (round + match) % n;
    int away = (round - match + n) % n;
    

    It should now be clearer that the home team is starting with the current round and then adding the match, while the away team is starting with the current round and subtracting the match. The % n makes it wrap around, and the + n for away makes it wrap around properly with negative numbers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a better way to write this spec? This works but I don't
I am passing 3 images in MultipartEntity . This works good, but I don't
Hello everyone don't know why this code works on windows but not on linux
This sql command works fine in sqlitemanager but in my android application this don't
I am trying to make a temperature map, all works fine but I don't
<tr> <td>type</td> <td>id=release_date</td> <td>2012-09-30</td> </tr> This works but I don't want to type the
I need to flash an element off and on. This works but I don't
For the normal ajax request I use: strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' But this don't work
This works but for some reason the comma is missing. data gets inserted as
(Citing source at: http://jqueryui.com/demos/dialog/#modal-form ) As an example, this works great but each time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.