Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Mathematical Challenge (Lottery)
Message
De
08/05/2003 11:42:16
 
 
À
07/05/2003 10:13:53
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00785796
Message ID:
00786288
Vues:
30
Or, even better:
**	Created by:		George E. Mamunes (george@mamunes.com)
**	Created on:		05/08/2003 11:11 AM
**	Purpose:		Creates a Cursor of Combinations

LOCAL i, j, n, la1(1), la2(1), llLoop, lcSolution, lnLen

n = 4		&& Number of elements

DIMENSION la1(n)		&& Array of possible values
la1(1) = '1'
la1(2) = '2'
la1(3) = '3'
la1(4) = '4'

NOTE: Create a cursor to hold the results:
lnLen = 0
FOR i = 1 TO n
	lnLen = lnLen + LEN(la1(i)) + 1
NEXT i
CREATE CURSOR cTemp (output C(lnLen), result I)
INDEX ON result TAG result

NOTE: Loop through each possible value:
FOR i = 0 TO (2^n) - 1	&& There are 2^n ways to combine n elements

	NOTE: This section is here because single and totally empty solutions are invalid in the example
	llLoop = .F.
	FOR j = 0 TO n-1
		IF i = 2^j OR i = 0		&& Each 1, 2, 4, 8, ..., etc. is a single entry in the chart
			llLoop = .T.
			EXIT
		ENDIF
	NEXT j

	IF llLoop
		LOOP
	ENDIF
	NOTE: ENDNOTE
	
	NOTE: Here we build an array of possible solutions:
	lnSoFar = i
	DIMENSION la2(n)
	la2 = .F.
	FOR j = (n - 1) TO 0 STEP -1
		IF lnSoFar >= 2^j
			lnSoFar = lnSoFar - 2^j
			la2(j + 1) = .T.
		ENDIF
	NEXT j
	
	NOTE: Here we pick up the elements from the original array, and concatenate them into a string:
	lcSolution = ''
	FOR j = 1 TO n
		IF la2(j)
			lcSolution = lcSolution + la1(j) + '+'
		ENDIF
	NEXT j
	IF RIGHT(lcSolution, 1) = '+'
		lcSolution = LEFT(lcSolution, LEN(lcSolution) - 1)
	ENDIF
	INSERT INTO cTemp VALUES (lcSolution, EVALUATE(lcSolution))
NEXT i
BROWSE NORMAL
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform