Five children: Jane, Kevin, Leo, Mike and Nina live in the same street in consecutively numbered houses.
(Addresses do not alternate from one side of the road
to another so they are all on the same side of the road).
The ages of the children are 5, 6, 9, 10 and 12 and the colours of their houses are red, yellow, green, blue and purple.
Using the following information determine each child’s information.
1.) The sum of their addresses is 55.
2.) The person who lives in the blue house has an age equal to half her address.
3.) The 9 year old lives in house No. 13.
4.) Leo’s age is the same as the number on the purple house.
5.) The colour of Jane’s house, combined with the colour of house No. 12, gives the colour of the 6 year old’s house.
6.) The Blue house is the same distance from the purple house as it is from the yellow house.
7.) 4 years ago Nina’s age was equal to twice as much as the difference between Kevin’s age (today) and the number on Mike’s house.
8.) One boy’s age is the same as his house number.
9.) The difference between Kevin and Mike’s house No.s is greater than the difference between Nina and Kevin’s age.
A fixed array was used to represent the ages, while strings represented the sets of house numbers (addresses), names and house colors, so that they could be permuted using a string permutation algorithm.
The colors are permuted first so that if neither purple nor green (the two secondary colors) is in the second position (the six-year-old's house) or if the first position (5-year-old's house) is not blue, the rest of the permutations are not done.
Requirement 9 is not tested by the program, but the output has only two sets of results and these can be tested manually.
DECLARE SUB permute (a$)
CLS
DIM age(5)
DATA 5,6,9,10,12
FOR i = 1 TO 5
READ age(i)
addr$ = addr$ + CHR$(8 + i) ' addresses 9 thru 13
NEXT
name$ = "jklmn"
clr$ = "rygbp"
'names, colors and addresses get permuted; ages are in fixed array
FOR hclr = 1 TO 120
IF MID$(clr$, 2, 1) = "g" OR MID$(clr$, 2, 1) = "p" THEN
' i.e., 6 yr old in secondary color hse
ixBlue = INSTR(clr$, "b")
IF ixBlue <= 2 THEN ' age in blue house must be 5 or 6
FOR hNo = 1 TO 120
IF MID$(addr$, ixBlue, 1) = CHR$(2 * age(ixBlue)) THEN
IF MID$(addr$, 3, 1) = CHR$(13) THEN '9-yr old hse 13
FOR nm = 1 TO 120
ixPrpl = INSTR(clr$, "p")
ixLeo = INSTR(name$, "l")
IF age(ixLeo) = ASC(MID$(addr$, ixPrpl, 1)) THEN
ixJane = INSTR(name$, "j")
clrJane$ = MID$(clr$, ixJane, 1)
ix12 = INSTR(addr$, CHR$(12))
clr12$ = MID$(clr$, ix12, 1)
good = 0
IF clrJane$ = "b" AND clr12$ = "y" AND MID$(clr$, 2, 1) = "g" THEN good = 1
IF clrJane$ = "y" AND clr12$ = "b" AND MID$(clr$, 2, 1) = "g" THEN good = 1
IF clrJane$ = "b" AND clr12$ = "r" AND MID$(clr$, 2, 1) = "p" THEN good = 1
IF clrJane$ = "r" AND clr12$ = "b" AND MID$(clr$, 2, 1) = "p" THEN good = 1
IF good THEN
ixYllw = INSTR(clr$, "y")
addrYllw = ASC(MID$(addr$, ixYllw, 1))
addrBlue = ASC(MID$(addr$, ixBlue, 1))
addrPrpl = ASC(MID$(addr$, ixPrpl, 1))
IF addrBlue + addrBlue = addrYllw + addrPrpl THEN
ixNina = INSTR(name$, "n")
ixKevin = INSTR(name$, "k")
ixMike = INSTR(name$, "m")
IF (age(ixNina) - 4) = 2 * ABS((age(ixKevin)) - ASC(MID$(addr$, ixMike, 1))) THEN
good = 0
FOR i = 1 TO 5
IF ASC(MID$(addr$, i, 1)) = age(i) AND INSTR("klm", MID$(name$, i, 1)) > 0 THEN
good = 1
END IF
NEXT
IF good THEN
FOR i = 1 TO 5
PRINT ASC(MID$(addr$, i, 1));
NEXT
PRINT " addrs"
FOR i = 1 TO 5
PRINT MID$(name$, i, 1);
NEXT
PRINT
FOR i = 1 TO 5
PRINT MID$(clr$, i, 1);
NEXT
PRINT
FOR i = 1 TO 5
PRINT age(i);
NEXT
PRINT " ages"
PRINT
END IF
END IF
END IF
END IF
END IF
permute name$
NEXT nm
END IF
END IF
permute addr$
NEXT hNo
END IF
END IF
permute clr$
NEXT hclr
The permute subroutine is shown elsewhere on the site.
The results are:
10 9 13 11 12 addrs
jmlnk
bpgyr
5 6 9 10 12 ages
10 9 13 11 12 addrs
jnlmk
bpgyr
5 6 9 10 12 ages
In the second of the two, mike and kevin live at houses 11 and 12, a difference of only 1, which is not greater than the 12 - 6 = 6 year difference in ages between Kevin and Nina. So the first set shown is the solution, with all strings in increasing order of the ages of the children.
|
Posted by Charlie
on 2005-10-09 15:46:16 |