One of the four people - Mr. Brown, his wife Monika, their son Mandy and their daughter Cindy - is a singer and another is a dancer.
- If the singer and the dancer are the same sex, then the dancer is older than the singer.
- If neither the singer nor the dancer is the parent of the other, then the singer is older than the dancer.
- If the singer is a man, then the singer and the dancer are the same age.
- If the singer and the dancer are of opposite sex then the man is older than the woman.
Whose occupation can you deduce with absolute certainty?
DECLARE SUB permute (a$)
CLS
' order MrBrown, wife Monika, son Mandy, daughter Cindy
occ$ = " sd"
ho$ = occ$
DO
FOR agetype = 1 TO 3 ' 1=singer younger; 2=equal ages; 3= singer older
good = 1
sing = INSTR(occ$, "s")
dance = INSTR(occ$, "d")
IF sing MOD 2 = dance MOD 2 AND agetype <> 1 THEN good = 0
IF (sing < 3) = (dance < 3) AND agetype <> 3 THEN good = 0
IF sing MOD 2 = 1 AND agetype <> 2 THEN good = 0
IF sing MOD 2 = 1 THEN manage = agetype
IF dance MOD 2 = 1 THEN manage = 4 - agetype
IF sing MOD 2 <> dance MOD 2 AND manage <> 3 THEN good = 0
IF sing < 3 AND dance > 2 AND agetype <> 3 THEN good = 0
IF dance < 3 AND sing > 2 AND agetype <> 1 THEN good = 0
IF good THEN PRINT ":"; occ$; ":"; agetype
NEXT agetype
permute occ$
LOOP UNTIL occ$ = ho$
SUB permute (a$)
DEFINT A-Z
x$ = ""
FOR i = LEN(a$) TO 1 STEP -1
l$ = x$
x$ = MID$(a$, i, 1)
IF x$ < l$ THEN EXIT FOR
NEXT
IF i = 0 THEN
FOR j = 1 TO LEN(a$) \ 2
x$ = MID$(a$, j, 1)
MID$(a$, j, 1) = MID$(a$, LEN(a$) - j + 1, 1)
MID$(a$, LEN(a$) - j + 1, 1) = x$
NEXT
ELSE
FOR j = LEN(a$) TO i + 1 STEP -1
IF MID$(a$, j, 1) > x$ THEN EXIT FOR
NEXT
MID$(a$, i, 1) = MID$(a$, j, 1)
MID$(a$, j, 1) = x$
FOR j = 1 TO (LEN(a$) - i) \ 2
x$ = MID$(a$, i + j, 1)
MID$(a$, i + j, 1) = MID$(a$, LEN(a$) - j + 1, 1)
MID$(a$, LEN(a$) - j + 1, 1) = x$
NEXT
END IF
END SUB
finds
: d s: 1
:d s: 1
Meaning that the daughter, Cindy, is the singer and either of the parents is the dancer.
|
Posted by Charlie
on 2013-06-17 18:23:07 |