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
Pregunta a los expertos en algoritmos
Pregunta a los expertos en algoritmos
SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
- nageswaragunupudi
- Posts: 8017
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Contact:
Re: Pregunta a los expertos en algoritmos
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.
Regards
G. N. Rao.
Hyderabad, India
G. N. Rao.
Hyderabad, India
Re: Pregunta a los expertos en algoritmos
Mr. Rao:
Thanks a lot, I Will try it.
Best regards
Thanks a lot, I Will try it.
Best regards
SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
Re: Pregunta a los expertos en algoritmos
Mr. Rao:
Excellent work, everything is fine, except when some value has decimals
Table
1350.10
2000.00
Target
3350.10
Best regards
Excellent work, everything is fine, except when some value has decimals
Table
1350.10
2000.00
Target
3350.10
Best regards
SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
- nageswaragunupudi
- Posts: 8017
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Contact:
Re: Pregunta a los expertos en algoritmos
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.
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.
Regards
G. N. Rao.
Hyderabad, India
G. N. Rao.
Hyderabad, India
Re: Pregunta a los expertos en algoritmos
Mr. Rao:
Bingo!, tank you very much.
Best regards
Bingo!, tank you very much.
Best regards
SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero