Pregunta a los expertos en algoritmos

Post Reply
User avatar
Armando
Posts: 2479
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México
Contact:

Pregunta a los expertos en algoritmos

Post 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
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
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Pregunta a los expertos en algoritmos

Post 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.
Regards

G. N. Rao.
Hyderabad, India
User avatar
Armando
Posts: 2479
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México
Contact:

Re: Pregunta a los expertos en algoritmos

Post by Armando »

Mr. Rao:

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
User avatar
Armando
Posts: 2479
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México
Contact:

Re: Pregunta a los expertos en algoritmos

Post 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
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
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Pregunta a los expertos en algoritmos

Post 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.
Regards

G. N. Rao.
Hyderabad, India
User avatar
Armando
Posts: 2479
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México
Contact:

Re: Pregunta a los expertos en algoritmos

Post by Armando »

Mr. Rao:

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
Post Reply