Suppose you have 27 weights weighing: 1², 2², 3², 4², ........, 25², 26² and 27² grams respectively.
(a)How can you group them into three groups so that each group has the same weight ?
(b) Is it possible to divide it into more than three groups satisfying the same conditions ?
(In reply to
part b by Charlie)
DECLARE SUB addIt ()
DEFLNG A-Z
CLS
DIM SHARED amt(27)
DIM SHARED used(27)
DIM SHARED noUsed
DIM SHARED toGo, ct
FOR i = 1 TO 27
amt(i) = i * i
PRINT i * i;
t = t + i * i
NEXT
PRINT t
tVal = 1386
bin271s = INT(2 ^ 27 - 1 + .5)
OPEN "27weigh5.txt" FOR OUTPUT AS #1
FOR i = 27 TO 1 STEP -1
PRINT i
noUsed = 1
used(1) = i
toGo = tVal - amt(i)
addIt
NEXT
PRINT ct
CLOSE
part2:
topN = 0
OPEN "27weigh5.txt" FOR INPUT AS #1
DO
INPUT #1, n
IF n >= INT(2 ^ 26 + .5) THEN topN = topN + 1: ELSE EXIT DO
LOOP
CLOSE
OPEN "27weigh5.txt" FOR BINARY AS #1
n = LOF(1) / 12
n$ = SPACE$(10)
FOR i = 1 TO n - 2
GET #1, (i - 1) * 12 + 1, n$
first = VAL(n$)
PRINT i
FOR j = topN + 1 TO n - 1
GET #1, (j - 1) * 12 + 1, n$
second = VAL(n$)
IF (first OR second) = first + second THEN
test = bin271s - first - second
IF test < second THEN
FOR k = j + 1 TO n
GET #1, (k - 1) * 12 + 1, n$
third = VAL(n$)
IF (first OR second OR third) = first + second + third THEN
test = bin271s - first - second - third
IF test < third THEN
FOR l = k + 1 TO n
GET #1, (l - 1) * 12 + 1, n$
fourth = VAL(n$)
IF (first OR second OR third OR fourth) = first + second + third + fourth THEN
test = bin271s - first - second - third - fourth
IF test < fourth THEN
num = first
GOSUB conv
IF LEN(nu$) < 27 THEN outFlag = 1: GOTO outOfIt
digi1$ = digi$
nu1$ = nu$
num = second
GOSUB conv
digi2$ = digi$
nu2$ = nu$
num = third
GOSUB conv
digi3$ = digi$
nu3$ = nu$
num = fourth
GOSUB conv
digi4$ = digi$
nu4$ = nu$
PRINT digi1$
PRINT RIGHT$(SPACE$(27) + nu1$, 27)
PRINT digi2$
PRINT RIGHT$(SPACE$(27) + nu2$, 27)
PRINT digi3$
PRINT RIGHT$(SPACE$(27) + nu3$, 27)
PRINT digi4$
PRINT RIGHT$(SPACE$(27) + nu4$, 27)
num = bin271s - first - second - third - fourth
GOSUB conv
PRINT digi$
PRINT RIGHT$(SPACE$(27) + nu$, 27)
ct = ct + 1
PRINT i; "/"; topN; ct
END IF
END IF
NEXT
END IF
END IF
NEXT
END IF
END IF
NEXT
IF outFlag THEN EXIT FOR
NEXT
outOfIt:
PRINT ct
CLOSE
END
conv:
nu$ = ""
dig = 1: digi$ = ""
DO
IF num MOD 2 = 1 THEN digi$ = digi$ + STR$(dig)
nu$ = LTRIM$(STR$(num MOD 2)) + nu$
num = INT(num / 2)
dig = dig + 1
LOOP UNTIL num = 0
RETURN
SUB addIt
FOR i = used(noUsed) - 1 TO 1 STEP -1
IF amt(i) < toGo THEN
noUsed = noUsed + 1
used(noUsed) = i
toGo = toGo - amt(i)
addIt
toGo = toGo + amt(i)
noUsed = noUsed - 1
ELSEIF amt(i) = toGo THEN
t = 0
FOR j = 1 TO noUsed
t = t + INT(2 ^ (used(j) - 1) + .5)
NEXT
t = t + INT(2 ^ (i - 1) + .5)
ct = ct + 1
PRINT #1, USING "##########"; t
END IF
NEXT
END SUB
|
Posted by Charlie
on 2003-05-31 11:12:42 |