Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Mathematics knundrum
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00780672
Message ID:
00781081
Vues:
23
>I'm working on a reconciliation project and i'm stuck. I have a list of numbers lets say 100 numbers and I have a target amount that some permutation of these numbers adds up to. I don't know how many of them will add up to this number. I need the list of numbers that adds up to my target number. I tried writing this but my brain overworked itself. Anybody ever write something like this or have any ideas? Thanks in advance!!!
>
>Example
>Say my number is 362.21 and my number list is:
>201.10
>25.18
>150.11
>778.88
>151.21
>11.31
>10.00
>...
>The answer is 201.10 150.11 and 10.00

Marvin --

Another solution, which is more scaleable, and is simple.

The hard part of this problem is all that's needed to exhaustively test the combinations.

But, there is a simple mechanism for us already. Consider the binary numbering system. Let's map each digit of a binary number of 100 digits to the 100 values that you need. Then we generate every number between 0 and 2 to the 100th. If we consider a binary digit as an on/off switch -- on to add the respective value to the sum, off to ignore it, we will generate all possible combinations of the values you have. For example, if you have 100.5 as the first value. If the 1st digit of our binary number is 1, then we add the value to the sum. If the 1st digit of our binary number is 0, we ignore it.

Then, to cycle through all the combinations all we have to do is start a mask value with 0 and increment by 1, add the values identified by the mask for that number, and cycle again until we reach the last number!

Below is a program which does just that:
LOCAL	llContinue, llAdd, llCarry, X, lnSum, lcValues

#DEFINE		nMASK	1
#DEFINE		nVALUE	2
#DEFINE		nVALUES	10
#DEFINE		nTARGET	15

DIMENSION aCombo	[nVALUES, 2]

aCombo [1, nValue] = 3
aCombo [2, nValue] = 6
aCombo [3, nValue] = 7
aCombo [4, nValue] = 2
aCombo [5, nValue] = 1
aCombo [6, nValue] = 4
aCombo [7, nValue] = 13
aCombo [8, nValue] = 12
aCombo [9, nValue] = 9
aCombo [10, nValue] = 10

llContinue = .T.
DO WHILE llContinue
	*	Test
	lnSum = 0
	FOR X = 1 TO nVALUES
		lnSum = lnSum ;
                       + IIF (aCombo[X, nMask],;
                         aCombo[X, nValue], 0)
	ENDFOR
	
	*	Output
	lcValues = ""
	IF lnSum = nTARGET
		FOR X = 1 TO nVALUES
			lcValues = lcValues ;
                                 + IIF (aCombo[X, nMask], ;
                                   TRANSFORM (aCombo[X, nValue]) + ",", ;
                                   "")
		ENDFOR
		lcValues = LEFT (lcValues, LEN (lcValues) - 1)
		WAIT WINDOW lcValues
	ENDIF
	
	* 	Increment mask
	
	llAdd = .T.
	FOR X = 1 TO nVALUES
		&&	If the current value is TRUE (a 1) and we're adding, set llCarry to .T.
		llCarry = llAdd AND aCombo [X, nMask]

		&&  Assign the current value using an XOR function (not in VFP for logicals)
		&&	Truth table:
		&&	aCombo	llAdd	aCombo next state
		&&	.F.		.F.		.F.
		&&	.F.		.T.		.T.
		&&	.T.		.F.		.T.
		&&	.T.		.T.		.F.		value is carried to next digit
		
		aCombo [X, nMask] = (aCombo [X, nMask] OR llAdd) AND NOT (aCombo [X, nMask] AND llAdd)
		
		&&	Setup for next iteration of loop
		llAdd = llCarry
	ENDFOR
	llContinue = NOT llCarry		&&	If we're carrying on the last digit, we're done
ENDDO
Enjoy!

Jay
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform