I have been asked to create round robin schedules for a 3v3 volleyball tournament where the following criteria must be met: (from most important to least important)
Each player plays with another no more than once
Each player gets to play at least 4 times
Each player cannot sit out for more than one round
Each player does not play against another player more
than twice
The question I am struggling to brute force is the case when I have:
40 players
4 courts
I’ve been able to brute force a semi functional solution for 15 teams but it’s become too unwieldy to do it by hand so I was thinking of trying a program in java. I am not entirely sure how I can go about making a brute force program to do this. I am currently attempting to use a list of every possible team of 3 and inserting them into the schedule. Then iterating over them replacing until the game works.
Here is another version of the same question asked by someone else
https://eso-community.net/viewtopic.php?t=9816
The top answer here has provided perfect solutions to teams of 8-13
The 40 player problem has a huge search space, and I don't believe that any brute force solution will work. Just think about selecting the 24 players who participate in one round, there are 6x10^10 ways of doing just that, and many many more ways of assigning those 24 players to 4 courts of 3 vs 3.
It's not clear exactly how may rounds you are looking for. With 4 courts of 6 players each, this leaves 16 byes per round. Therefore any multiple (i) of 5 rounds is very desirable, since 16 x 5i = 80i, so it is possible to arrange that for every 5i rounds played, each of the 40 teams has exactly 3i games and 2i byes. You have asked for "each player gets to play at least 4 times", so the best option is to look for a solution with 10 rounds, where each player has 6 games. Here is one possible option where all partners are different, and all opponents are different.
(38 15 11 v 7 25 23) (33 14 9 v 20 29 19) (40 16 27 v 37 28 21) (34 17 3 v 10 24 32)
(39 11 12 v 8 21 24) (34 15 10 v 16 30 20) (36 17 28 v 38 29 22) (35 18 4 v 6 25 33)
(40 12 13 v 9 22 25) (35 11 6 v 17 26 16) (37 18 29 v 39 30 23) (31 19 5 v 7 21 34)
(36 13 14 v 10 23 21) (31 12 7 v 18 27 17) (38 19 30 v 40 26 24) (32 20 1 v 8 22 35)
(37 14 15 v 6 24 22) (32 13 8 v 19 28 18) (39 20 26 v 36 27 25) (33 16 2 v 9 23 31)
(36 5 33 v 12 32 15) (13 7 24 v 20 4 2) (25 1 28 v 30 10 31) (37 26 38 v 3 9 18)
(37 1 34 v 13 33 11) (14 8 25 v 16 5 3) (21 2 29 v 26 6 32) (38 27 39 v 4 10 19)
(38 2 35 v 14 34 12) (15 9 21 v 17 1 4) (22 3 30 v 27 7 33) (39 28 40 v 5 6 20)
(39 3 31 v 15 35 13) (11 10 22 v 18 2 5) (23 4 26 v 28 8 34) (40 29 36 v 1 7 16)
(40 4 32 v 11 31 14) (12 6 23 v 19 3 1) (24 5 27 v 29 9 35) (36 30 37 v 2 8 17)
Related
I have two sheets in an Excel workbook. I need the formula which creates tables by using vlookup.
I have 10 columns and 10 rows like this
1 2 3 4 5 6 7 8 9 10
2
3
4
5
6
7
8
9
10
I have tried to use Vlookup with sum but not get the actual results.
The expected result should be like this
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 42 48 56 64 72 80
9 18 27 36 49 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
I'm trying to print the following table in C++:
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
5 5 10 15 20 25 30 35 40 45 50
6 6 12 18 24 30 36 42 48 54 60
7 7 14 21 28 35 42 49 56 63 70
8 8 16 24 32 40 48 56 64 72 80
9 9 18 27 36 45 54 63 72 81 90
10 10 20 30 40 50 60 70 80 90 100
Using nested while loops only.
I have two main problems here:
I don't understand how to do it without using a non-nested while at the beginning (to print the first line), or an if statement for the first line.
Using setw I have problems trying to align the numbers with two digits.
Here is what I tried
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int k=0;
while(k<=10)
{cout << k << setw(5);
k++;
};
cout << "\n";
int i=1;
while(i<=10){
cout << i << setw(5);
int j=1;
while(j<=10){
cout<< i*j << setw(5);
j++;
}
cout << "\n";
i++;
}
return 0;
}
But, as said, I used a non-nested while at the beginning, and also the output is:
0 1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
5 5 10 15 20 25 30 35 40 45 50
6 6 12 18 24 30 36 42 48 54 60
7 7 14 21 28 35 42 49 56 63 70
8 8 16 24 32 40 48 56 64 72 80
9 9 18 27 36 45 54 63 72 81 90
10 10 20 30 40 50 60 70 80 90 100
Where the two-digits numbers are not aligned in the proper way. On the other hand I cannot think a way to modify the loop in order to increase the space only for two digits number, without using an if statement.
So am I missing something or it is not possible to print the table above without using if or non-nested while?
I think you want to hide first 0 value. To hide it, I have used some bit manipulations. There is no if statement and all of the printing in nested while.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i = 0, k = 0;
while (i < 10){
int j = 0;
while (j <= 10){
cout << left << setw(5);
(i || j || k) && cout << j + i * j + !j * (i + 1);
!(i || j || k) && cout << "";
j++;
}
cout << "\n";
i += k++ > 0;
}
return 0;
}
The output is
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
5 5 10 15 20 25 30 35 40 45 50
6 6 12 18 24 30 36 42 48 54 60
7 7 14 21 28 35 42 49 56 63 70
8 8 16 24 32 40 48 56 64 72 80
9 9 18 27 36 45 54 63 72 81 90
10 10 20 30 40 50 60 70 80 90 100
Explanation:
I have used j for seed and i * j for new value for (j + 1)-th column.
It works perfectly for from first row second column to last row last column.
For first column, j is always 0. To make it work, I used !j * (i + 1) which prints 0 to 10 for first column.
To hide 0 for first row and first column, all of i, j and k are 0. For all other cells, at least one of them has value other than 0.
If still unclear, I'll explain with example.
Things to consider:
Outputting tabs instead of trying to set the width is much easier,
though you'll have less control over how your output looks (it will
depend in the tab stop width of your console). But your standard tab stops
will handle your range of values.
your problem statement doesn't say how many while loops you can use
You can use a while loop to emulate an if statement like this:
while(j == 0){
cout << i << '\t';
break;
}
I'll leave the rest as an exercise for you.
Hi so I have created the nested list/matrix:
q)m:((1 2 3);(4 5 6);(7 8 9))
q)m
1 2 3
4 5 6
7 8 9
I have also identified the middle column in the list:
q)a:m[0;1],m[1;1],m[2;1]
I now want to replace the middle row (4 5 6) with a to finish with m looking like:
q)m
1 2 3
2 5 8
7 8 9
You've already seen you can index into the matrix with syntax like m[0;1], where 0 refers to the first level of nesting and 1 refers to the second level
KDB also allows you to assign to an index of a list in a similar way e.g.
q)l:1 2 3 4
q)l[1]:20
q)l
1 20 3 4
So you can use something similar in this example:
q)m[1]:a
q)m
1 2 3
2 5 8
7 8 9
As an aside, KDB also allows you to leave out an index, in which case it will take all items from the corresponding level of nesting, e.g.
q)m[0] /first level of nesting i.e. first row
1 2 3
q)m[;0] /second level of nesting i.e. first column
1 4 7
Hope that helps
Jonathon McMurray
AquaQ Analytics
You want to generalise for larger matrices (which must also be square) so your answer needs two parts:
how to construct a
how to insert it
for row/col x where x<count m.
The general expression you want is simply m[x;]:m[;x], because m[x;] denotes row x and m[;x] denotes column x.
See Q for Mortals 3.11.3 Two- and Three-Dimensional Matrices
You can make this a function of the index and the matrix:
q)show m:5 5#1_til 26
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
q){y[x;]:y[;x];:y}[3;m]
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
4 9 14 19 24
21 22 23 24 25
Just adding another approach for you.
q)m:8 cut til 64
q)0 0+\:til 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
q)(m)./:flip 0 0+\:til 8
0 9 18 27 36 45 54 63
q)#[m;4;:;(m)./:flip 0 0+\:til 8]
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31
0 9 18 27 36 45 54 63
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
q)
For fun, here it is in a function which takes the length&width of the matrix and replaces the 'middle' row with the diagonal values
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}8
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31
0 9 18 27 36 45 54 63
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}5
0 1 2 3 4
5 6 7 8 9
0 6 12 18 24
15 16 17 18 19
20 21 22 23 24
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}4
0 1 2 3
4 5 6 7
0 5 10 15
12 13 14 15
q)
q)#[((1 2 3);(4 5 6);(7 8 9));1;:;(2;5;8)]
1 2 3
2 5 8
7 8 9
Indexing in q can be straight forward and I believe a intermediate can be omitted:
q)m:((1 2 3);(4 5 6);(7 8 9))
q)m[1]:m[;1]
q)m
1 2 3
2 5 8
7 8 9
OK, so here is what i got going on. I use the latest version of Clion by JetBrains. I run the MinGW GUI as a compiler and use the bundled CMake that comes with the GUI.
So here is my code.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 30; i++) {
for (int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << " LINE: " << i;
cout << endl;
}
return 0;
}
Very simple and this is what it should output
1 LINE: 1
1 2 LINE: 2
1 2 3 LINE: 3
1 2 3 4 LINE: 4
And it continues up until 30.
I know that the actual code is correct because when I run it on a different compiler (On an Ubuntu VM) it comes out perfectly. But when I run it on Clion this is the output:
1 LINE: 1
1 2 LINE: 2
1 2 3 LINE: 3
1 2 3 4 LINE: 4
1 2 3 4 5 LINE: 5
1 2 3 4 5 6 LINE: 6
1 2 3 4 5 6 7 LINE: 7
1 2 3 4 5 6 7 8 LINE: 8
1 2 3 4 5 6 7 8 9 LINE: 9
1 2 3 4 5 6 7 8 9 10 LINE: 10
1 2 3 4 5 6 7 8 9 10 11 LINE: 11
1 2 3 4 5 6 7 8 9 10 11 12 LINE: 12
1 2 3 4 5 6 7 8 9 10 11 12 13 LINE: 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 LINE: 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 LINE: 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 LINE: 16
1 2 3 4 5 6 71 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 LINE: 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 LINE: 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 LINE: 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 LINE: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 LINE: 21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 LINE: 22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 LINE: 23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 LINE: 24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 LINE: 25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 LINE: 26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 LINE: 27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 181 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 LINE: 28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 LINE: 29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 LINE: 30
Ya, so that clearly doesn't work. And and to solidify the fact that its not my code, when I run it again...
1 LINE: 1
1 2 LINE: 2
1 2 3 LINE: 3
1 2 3 4 LINE: 4
1 2 3 4 5 LINE: 5
1 2 3 4 51 2 3 4 5 6 LINE: 6
1 2 3 4 5 6 7 LINE: 7
1 2 3 4 5 6 7 8 LINE: 8
1 2 3 4 5 6 7 8 9 LINE: 9
1 2 3 4 5 6 7 8 9 10 LINE: 10
1 2 3 4 5 6 7 8 9 10 11 LINE: 11
1 2 3 4 5 6 7 8 9 10 11 12 LINE: 12
1 2 3 4 5 6 7 8 9 10 11 12 13 LINE: 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 LINE: 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 LINE: 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 LINE: 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 LINE: 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 LINE: 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 LINE: 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 LINE: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 LINE: 21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 LINE: 22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 LINE: 23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 LINE: 24
1 2 3 4 5 6 71 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 LINE: 25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 LINE: 26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 LINE: 27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 LINE: 28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 LINE: 29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 LINE: 30
And if ran it again it would once again be different. It never the same and is always doing the same thing more or less.
This issue only seems to exist when I run programs that spit out long cout's. For example the HelloWorld runs fine. And as the last kick in the nuts, I ran it on my other computer (same setup with Clion and MinGW) and the exact same thing was happening.
This question was asked in an interview, can someone tell what does the following code do? It gives output 15 for 150, 3 for 160, 15 for 15. What mathematical operation is it performing on 'n'.
int foo(int n)
{
int t,count=0;
t=n;
while(n)
{
count=count+1;
n=(n-1)&t;
}
return count;
}
It seems to calculate the number max(n**2-1, 0), where n is the number of 1 bits in a number's binary representation:
0 0 0b0
1 1 0b1
2 1 0b10
3 3 0b11
4 1 0b100
5 3 0b101
6 3 0b110
7 7 0b111
8 1 0b1000
9 3 0b1001
10 3 0b1010
11 7 0b1011
12 3 0b1100
13 7 0b1101
14 7 0b1110
15 15 0b1111
16 1 0b10000
17 3 0b10001
18 3 0b10010
19 7 0b10011
20 3 0b10100
21 7 0b10101
22 7 0b10110
23 15 0b10111
24 3 0b11000
25 7 0b11001
26 7 0b11010
27 15 0b11011
28 7 0b11100
29 15 0b11101
30 15 0b11110
31 31 0b11111
32 1 0b100000
33 3 0b100001
34 3 0b100010
35 7 0b100011
36 3 0b100100
37 7 0b100101
38 7 0b100110
39 15 0b100111
40 3 0b101000
41 7 0b101001
42 7 0b101010
43 15 0b101011
44 7 0b101100
45 15 0b101101
46 15 0b101110
47 31 0b101111
48 3 0b110000
49 7 0b110001
50 7 0b110010
51 15 0b110011
52 7 0b110100
53 15 0b110101
54 15 0b110110
55 31 0b110111
56 7 0b111000
57 15 0b111001
58 15 0b111010
59 31 0b111011
60 15 0b111100
61 31 0b111101
62 31 0b111110
63 63 0b111111
64 1 0b1000000
65 3 0b1000001
66 3 0b1000010
67 7 0b1000011
68 3 0b1000100
69 7 0b1000101
70 7 0b1000110
71 15 0b1000111
72 3 0b1001000
73 7 0b1001001
74 7 0b1001010
75 15 0b1001011
76 7 0b1001100
77 15 0b1001101
78 15 0b1001110
79 31 0b1001111
80 3 0b1010000
81 7 0b1010001
82 7 0b1010010
83 15 0b1010011
84 7 0b1010100
85 15 0b1010101
86 15 0b1010110
87 31 0b1010111
88 7 0b1011000
89 15 0b1011001
90 15 0b1011010
91 31 0b1011011
92 15 0b1011100
93 31 0b1011101
94 31 0b1011110
95 63 0b1011111
96 3 0b1100000
97 7 0b1100001
98 7 0b1100010
99 15 0b1100011
It is easier to find out the "mathematical operation", when function is changed to recursive:
int foo(int n, int t)
{
if( n )
return foo( (n-1) & t ) + 1
else
return 0;
}
So formula is:
F(0,t) = 0
F(n,t) = F( (n-1) & t, t ) + 1
foo(n) = F(n,n)
I don't have any idea, is that wellknown formula for counting something, or not.
You may find answer from math.stackexchange.com
That is a method known as Brian Kernighan's way to count set bits :
unsigned int v; // count the number of bits set in v
unsigned int c; // c accumulates the total bits set in v
for (c = 0; v; c++)
{
v &= v - 1; // clear the least significant bit set
}
Brian Kernighan's method goes through as many iterations as there are set bits. So if we have a 32-bit word with only the high bit set, then it will only go once through the loop.
Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie) mentions this in exercise 2-9. On April 19, 2006 Don Knuth pointed out to me that this method "was first published by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and published in 1964 in a book edited by Beckenbach.)"