+---------+-------+
| 8 9 5 1 | R R |
+---------+-------+
| 2 1 6 9 | R B |
+---------+-------+
| 3 6 9 4 | R B |
+---------+-------+
| 4 7 2 1 | R B |
+---------+-------+
| 1 2 3 7 | R R R |
+---------+-------+
Each of the rows in the table given above indicates an attempt to find
out the secret number. Each try has, in the column to the right, the
letters R and B.
Each R indicates that this number has one digit in common with the secret
number, but in a different position.
Each B indicates that this number has one digit in common with the secret
number in the same position.
Determine the secret number.
DECLARE FUNCTION ans$ (a$, b$)
DATA 8951,rr, 2169,rb, 3694,rb, 4721,rb, 1237,rrr
FOR i = 1 TO 5
READ try$(i), resp$(i)
NEXT
FOR n = 0 TO 9999
cd$ = RIGHT$("00000" + LTRIM$(STR$(n)), 4)
good = 1
FOR i = 1 TO 5
IF ans$(cd$, try$(i)) <> resp$(i) THEN good = 0: EXIT FOR
NEXT
IF good THEN PRINT cd$
NEXT
FUNCTION ans$ (a$, b$)
an$ = "": x1$ = "": x2$ = ""
FOR i = 1 TO LEN(b$)
IF MID$(a$, i, 1) = MID$(b$, i, 1) THEN
an$ = an$ + "b"
ELSE
x1$ = x1$ + MID$(a$, i, 1)
x2$ = x2$ + MID$(b$, i, 1)
END IF
NEXT
FOR i = 1 TO LEN(x2$)
ix = INSTR(x1$, MID$(x2$, i, 1))
IF ix > 0 THEN
an$ = "r" + an$
x1$ = LEFT$(x1$, ix - 1) + MID$(x1$, ix + 1)
END IF
NEXT
ans$ = an$
END FUNCTION
finds
3719
|
Posted by Charlie
on 2013-04-02 16:01:17 |