Professional Documents
Culture Documents
(Five Jack) Online Test Fix
(Five Jack) Online Test Fix
In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers.
For any positive integer n, define d(n) to be n plus the sum of the digits of n.
Some numbers have more than one generator: for example, 101 has two generators, 91 and 100.
There are thirteen self-numbers less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97.
- Write a program to sum of all self-numbers which are bigger than 0 and smaller than 5000.
Answer :
<?php
$notSelfNumbers = array();
$allNumbers = array();
$b=$i;
$b= (string)$b ;
$allNumbers[] = $i;
$sumOfThisNumbers = $i+$b[0];
$sumOfThisNumbers = $i+$b[0]+$b[1];
$sumOfThisNumbers = $i+$b[0]+$b[1]+$b[2];
$notSelfNumbers[] = $sumOfThisNumbers; // sum of this 3 number
$sumOfThisNumbers = $i+$b[0]+$b[1]+$b[2]+$b[3];
// let's compare the numbers and find what we only need to show.
$sumSelfNumbers = 0;
$numbering = 1;
foreach($selfNumbers as $eachSelfNumber) {
echo "<pre>Sum of all self-numbers from bigger than 1 and smaller than 5000 is: <strong>" .
number_format($sumSelfNumbers)."</strong>";
?>
So with this code, I will show which are the "self-numbers" and SUM of them (bigger than 0 and
smaller than 5000)
There is a room with n rows and n columns, and there are gunmen in the room.
A gunman can see vertical or horizontal way, and cannot see over the wall or diagonal.
If a gunman see another gunman, they will shoot each other and only one man will survive.
■: Wall
□: Empty space
♂: Gunman
■■■□
□□□□
□■□□
■■■□
■■■□
□□□♂
♂■♂□
■■■□
However, we cannot place gunmen like below, because they are in the same line horizontally, one
man will die.
■■■□
□♂□♂
□■□□
■■■□
We can place maximum 4 gunmen in this room, and there are 2 ways to achieve this.
■■■♂
□♂□□
♂■♂□
■■■□
■■■□
□♂□□
♂■♂□
■■■♂
- Answer :
max_num_gunmen = 0
len_board = 0
global len_board
# place a gunman
board[row][col] = 1
if board[row + delta][col] == 1:
break
board[row + delta][col] = 1
if board[row - delta][col] == 1:
break
board[row - delta][col] = 1
if board[row][col + delta] == 1:
break
board[row][col + delta] = 1
if board[row][col - delta] == 1:
break
board[row][col - delta] = 1
global max_num_gunmen
global len_board
for i in range(len_board):
for j in range(len_board):
if board[i][j] == 1:
continue
# store gunman position and board before place a gunman
# place a gunman
num_gunmen += 1
else:
if not board_stack:
return
# backtracking
board[i][j] = 1
num_gunmen -= 1
def solution(board):
global len_board
for i in range(len_board):
for j in range(len_board):
if board[i][j] == 1:
continue
□■□■\n\
■□■□\n\
□■□■\n\
■□■□\
"
gunmen_board = []
len_board = len(gunmen_board)
solution(gunmen_board)
print(max_num_gunmen)
Answer : 13
Answer : 3 ways
1. ♂■♂■♂■♂■
□□□□♂■□♂
■♂■□□■♂■
□□□♂□□□□
□□♂■□□□□
□□□□□■♂■
□■♂□□□□□
□□□♂■□■□
2. ♂■□■□■♂■
□□♂□□■□♂
■□■♂□■□■
□♂□□□□□□
□□□■□□□□
□□♂□□■♂■
□■□□□♂□□
□□□□■♂■□
3. □■♂■♂■♂■
♂□□□□■♂□
■♂■♂□■□■
□□□□□□□♂
□□□■♂□□□
□□□□♂■□■
♂■□□□□□□
□□□♂■□■♂
□□□□□■□□
■□■□□■□■
□□□□□□□□
□□□■□□□□
□□□□□■□■
□■□□□□□□
□□□□■□■□