When I first got acquainted with programming, my brother threw an exe of a self-written program. He lost the sources, but I want to recreate this program.

Its essence is as follows:

Three columns with random numbers are given. You make one number and enter the number of the column in which it is located. Then three more columns appear, with the same numbers, but scattered differently. Again, select the column in which your number. And so again. And then the program guesses the figure that you made. In general, I can not imagine how to implement it. I know that this is an absolutely primitive console program, but I'm still learning :)

Give at least some clues: algorithm, sample code, links. I will be grateful to everything.

  • 3
    If N columns are N digits, then everything is very simple - it is necessary to output the columns line by line on the second move. The number is determined unequivocally :) - Harry
  • This is an array with the variable switch. If it is more convenient, then if else - Denis SafonoV
  • 2
    There is a similar card trick - " ru.m.wikihow.com/… " It can help with the implementation) - Pavel Parshin
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

Given 3 columns and 3 iterations. Variants of placement of one number so that it would be possible to determine the number by placement - nine: 000 (the number is always in place), 001, 002, 010, 011, 012, 020, 021, 022.

We take 9 numbers for each column. We can fill an array of 27 elements with random, unique numbers. Further, in the description of the algorithm, by number we mean its position in this array, starting from 0. The columns "on the screen" will be numbered 0, 1, 2. At the first iteration, the first 9 numbers are entered in the 0 column , the second in the 1st, the third in the 2nd . When placed on the second iteration, we take the remainder before dividing the number by 9 (in fact, this is the number of the number in its group of the first iteration). We divide this remainder completely by 3 — this is how many columns you need to shift the number at the second iteration relative to the initial position ( смещение = (X % 9) / 3 ). If the number of the original column + offset is greater than 2, then we start the counting from the beginning (ie, do 2-(K1+смещение) ) At the third iteration, take the remainder of the division by 3 ( смещение=X % 3 ). With this approach, for each number we get the arrangements indicated at the very beginning of the post.

Guessing: Multiply the column number of the first iteration by 9, add the column number of the second iteration multiplied by 3, add the column number of the third iteration ( K1*9+K2*3+K3 ). All, got a number (well, that is, its position in the initial array).

Example:

 1я итерация 2я итерация 3я итерация 0 1 2 0 1 2 0 1 2 0 9 18 0 3 6 0 1 2 1 10 19 1 4 7 3 4 5 2 11 20 2 5 8 6 7 8 3 12 21 9 12 15 9 10 11 4 13 22 10 13 16 12 13 14 5 14 23 11 14 17 15 16 17 6 15 24 18 21 24 18 19 20 7 16 25 19 22 25 21 22 23 8 17 26 20 23 26 24 25 26 

User entered: 2nd, 1st, 0th: 2*9+1*3+0=21 , entered 1st, 2nd, 1st: 1*9+2*3+1=16

The contents of the columns at each iteration can be mixed, so that no regularities in the distribution can be seen.

Direct printing of indices of the original array on php (formulas are revised for the case of printing all three iterations into a string, as in the example above):

 for($i=0;$i<9;$i++) { // Строка for($j=0;$j<3;$j++) { // Итерация for($k=0;$k<3;$k++) { // Колонка if($j==0) $m=$i+9*$k; elseif($j==1) $m=(int)($i/3)*9+$i%3+$k*3; else $m=$i*3+$k; printf("%3d",$m); } print " "; } print "\n"; } 
  • Thank you so much. Without you, I would never have guessed. By the way, this algorithm, can I read about it somewhere (is there an article on the Wiki, as it is called at all)? Or did you come up with it yourself? :) - afishr
  • Simply, honestly, I didn’t fully understand what these accommodation options mean 000 (the number is always in place), 001, 002, 010, 011, 012, 020, 021, 022. - afishr
  • Look at the table. Numbers 0, 13 and 26 in all three cases in the same column. In each column, 1 such number is 000. The numbers 1, 14, 24 are the first two times in their column, the last time in the next is 001. Pay attention to the “next” column of the number 24 - it should move to the right and get into the nonexistent 4th column, respectively, it "turned around" and got into the first one. Numbers 8, 9, 22 with respect to their initial location moved by 2 to the right in the 2nd and 3rd iteration - here they are 022 - Mike
  • By the way, you can simply take an array of 27 numbers, say arr, fill it with random, unique numbers and an array of columns col [3] [9]. use my print cycles that instead of printing copy for each iteration (variable j) an array of columns col[k][i]=arr[m] , mix the values ​​within each of the 3 col subarrays and print them. When guessing the formula, consider the position and output the number from arr [ number ] - Mike
  • There was such a trick with cards :) - pegoopik