Advertisement
Guest User

Кодовый замок

a guest
Sep 15th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.81 KB | None | 0 0
  1. class Lock
  2. {
  3.     public $n;
  4.     public $attempts = 1;
  5.     public $combinations = [];
  6.  
  7.     function __construct($n = 1) {
  8.         $this->n = $n;
  9.  
  10.         $c = 0;
  11.  
  12.         for($row = 0; $row < 2**$n; $row++) {
  13.             $r = [];
  14.             $sum = 0;
  15.             for ($col = 0; $col < $n; $col++) {
  16.                 $r[$col] = ($c >> ($col)) % 2;
  17.                 $sum += $r[$col];
  18.             }
  19.  
  20.             $this->combinations[$sum][] = $r;
  21.             $c++;
  22.         }
  23.  
  24.         $max = 0;
  25.         for($row = 0; $row < $n; $row++) {
  26.             $len = count($this->combinations[$row]);
  27.             if($len > $max)
  28.                 $max = $len;
  29.         }
  30.  
  31.         $this->attempts = $max;
  32.     }
  33. }
  34.  
  35.  
  36. ......
  37.  
  38.     for ($n = 2; $n <= 10; $n++){
  39.         $lock = new Lock($n);
  40.         dump($lock);
  41.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement