Number allocation

Problem: we have x checkboxes and we want to check them evenly.

Example 1: select 50 checkboxes from 100.

[-]
[x]
[-]
[x]
...

Example 2: Select 33 checkboxes from 100.

[-]
[-]
[x]
[-]
[-]
[x]
...

Example 3: select 66 flags from 100:

[-]
[x]
[x]
[-]
[x]
[x]
...

But we had problems with the application of the formula for checking their code, especially after you go 11/111 or something like that. Anyone have an idea?

+5
source share
8 answers

Here's a simple solution using integer arithmetic:

void check(char boxes[], int total_count, int check_count)
{
    int i;

    for (i = 0; i < total_count; i++)
        boxes[i] = '-';

    for (i = 0; i < check_count; i++)
        boxes[i * total_count / check_count] = 'x';
}

total_count- the total number of boxes, and check_count- the number of checks.

Firstly, it disables all checkboxes. Then he checks the flags check_count, scaling the counter by the number of boxes.

: , , . , x--x--, --x--x. ,

        boxes[i * total_count / check_count] = 'x';

:

        boxes[total_count - (i * total_count / check_count) - 1] = 'x';

0 <= check_count <= total_count, boxes total_count , , :

  • . i * total_count / check_count , total_count >= check_count.

  • . i * total_count / check_count

    • >= 0. i, total_count, check_count >= 0.

    • < total_count. n > 0 d > 0:

      (n * d - 1) / d < n
      

      , n * d / d , .

      , (check_count - 1) * total_count / check_count total_count, , . , check_count 0, .

+2

, y x. p = y/x, . , p, 1 .

, , r = y%x . p = y/x, / . , :

  • p-r 1
  • r 2

.. , . , r x+1 p-r x, .

, . , :

:

  • if y > 2*x, 1 p = y/x , x .
  • if y < 2*x, , y-x y (, , x y-x)

.. , . , p p+1, .

+4

C, Xes N.

, C = 111 N = 11 .

: C/N. D. I. , M.

double D = (double)C / (double)N;
double I = 0.0;
int M = N;
while (M > 0) {
    if (checkboxes[Round(I)].Checked) { //  if we selected it, skip to next
        I += 1.0;
        continue;
    }
    checkboxes[Round(I)].Checked = true;
    M --;
    I += D;
    if (Round(I) >= C) { //  wrap around the end
        I -= C;
    }
}

, Round (x) x.

.

+1

, , , .

, , 33 100 . 100 / 33 = 3.030303..., 3.030303... . , 3.030303... , . 66 100 1,51515... , 11 111 10.090909... ..

double count = 0;
for (int i = 0; i < boxes; i++) {
    count += 1;
    if (count >= boxes/checks) {
        checkboxes[i] = true;
        count -= count.truncate(); // so 1.6 becomes 0.6 - resetting the count but keeping the decimal part to keep track of "partial boxes" so far
    }
}

double count decimal, , - .

+1

Bresenham - . "x" Y-. err [0..places].

def Distribute(places, stars):
err = places // 2
res = ''
for i in range(0, places):
    err = err - stars
    if err < 0 :
        res = res + 'x'
        err = err + places
    else:
        res = res + '-'
print(res)

Distribute(24,17)
Distribute(24,12)
Distribute(24,5)

output:

x-xxx-xx-xx-xxx-xx-xxx-x

-x-x-x-x-x-x-x-x-x-x-x-x

--x----x----x---x----x--
+1

html/javascript:

<html>
<body>
<div id='container'></div>
<script>
var cbCount = 111;
var cbCheckCount = 11;
var cbRatio = cbCount / cbCheckCount;
var buildCheckCount = 0;

var c = document.getElementById('container');

for (var i=1; i <= cbCount; i++) {
  // make a checkbox
  var cb = document.createElement('input');
  cb.type = 'checkbox';

  test = i / cbRatio - buildCheckCount;
  if (test >= 1) {
    // check the checkbox we just made
    cb.checked = 'checked';
    buildCheckCount++;
  }

  c.appendChild(cb);
  c.appendChild(document.createElement('br'));
}
</script>
</body></html>
0

. N = x = M = y = , , (N*i+N)/M - (N*i)/M . ( . .)

python :

  N=100; M=33; p=0;
  for i in range(M):
     k = (N+N*i)/M
     for j in range(p,k-1): print "-",
     print "x",
     p=k


- - x - - x - - x - - x - - [...] x - - x - - - x
[...] 25 --x . M=66
x - x x - x x - x x - x x - [...] x x - x x - x - x
[...] xx- , x- .

, C java: for (i=0; i<M; ++i) for i in range(M):. for (j=p; j<k-1; ++j) for j in range(p,k-1):.

: , M = x , print "x", M .

0

Fisher-Yates shuffle?

Make an array, shuffle and select the first n elements. You do not need to shuffle all of them, only n arrays first. Shuffle can be found in most language libraries.

0
source

All Articles