Page 1 of 1
Pregunta a los expertos en algoritmos
Posted: Tue Jul 28, 2020 12:57 am
by Armando
Hola Amigos:
Uno de mis clientes pide hacer lo siguiente, (Caprichos de los clientes):
Tengo una tabla con los siguientes importes aleatorios
$3,500.10
$1,789.00
$5,987.00
$9,875.00
$2,547.00
Después el usuario registra un valor de $4,336.00
y el prg debe determinar con que importes de la tabla llego a la misma suma registrada,
en este ejemplo seria con $1,789.00 + 2,547.00
Alguien se anima?
Saludos
Re: Pregunta a los expertos en algoritmos
Posted: Tue Jul 28, 2020 11:08 am
by nageswaragunupudi
Code: Select all
#include "fivewin.ch"
function Main()
local aData := { 3500, 1789, 5987, 9875, 2547 }
local aSorted, aResult, nTarget := 4336
aSorted := AClone( aData )
ASort( aSorted, nil, nil, { |x,y| x > y } )
nTarget := 5289
nTarget := 10323
nTarget := 6047
nTarget := 7836
aresult := {}
if findItems( ntarget, asorted, 1, aResult )
? FW_ArrayAsList( aResult, "+" ) + "=" + cValToChar( FW_ArrSum( aResult ) )
else
? "Not found"
endif
return nil
function FindItems( nSeek, aSeek, nFrom, aFound )
local nAt, nVal, nLen
for nAt := nFrom to Len( aSeek )
nVal := aSeek[ nAt ]
if nVal == nSeek
AAdd( aFound, nVal )
return .t.
endif
if nVal < nSeek
nLen := Len( aFound )
AAdd( aFound, nVal )
if FindItems( nSeek - nVal, aSeek, nAt + 1, aFound )
return .t.
else
ASize( aFound, nLen )
endif
endif
next
return .f.
Re: Pregunta a los expertos en algoritmos
Posted: Tue Jul 28, 2020 2:41 pm
by Armando
Mr. Rao:
Thanks a lot, I Will try it.
Best regards
Re: Pregunta a los expertos en algoritmos
Posted: Tue Jul 28, 2020 3:05 pm
by Armando
Mr. Rao:
Excellent work, everything is fine, except when some value has decimals
Table
1350.10
2000.00
Target
3350.10
Best regards
Re: Pregunta a los expertos en algoritmos
Posted: Tue Jul 28, 2020 4:56 pm
by nageswaragunupudi
I intended the algorithm for integers.
If you want to use values with 2 decimal places, then I suggest you make the array of Int( eachvalue * 100 ).
Also, convert the target value to Int( nTarget * 100 )
After finding items, divide by 100 and show it to the user.
Algorithms like this work reliably with integers.
Re: Pregunta a los expertos en algoritmos
Posted: Tue Jul 28, 2020 6:29 pm
by Armando
Mr. Rao:
Bingo!, tank you very much.
Best regards