FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Pregunta a los expertos en algoritmos
Posts: 3358
Joined: Fri Oct 07, 2005 08:20 PM
Pregunta a los expertos en algoritmos
Posted: Tue Jul 28, 2020 12:57 AM

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
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Pregunta a los expertos en algoritmos
Posted: Tue Jul 28, 2020 11:08 AM
Code (fw): Select all Collapse
#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
Posts: 3358
Joined: Fri Oct 07, 2005 08:20 PM
Re: Pregunta a los expertos en algoritmos
Posted: Tue Jul 28, 2020 02:41 PM

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
Posts: 3358
Joined: Fri Oct 07, 2005 08:20 PM
Re: Pregunta a los expertos en algoritmos
Posted: Tue Jul 28, 2020 03:05 PM

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
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Pregunta a los expertos en algoritmos
Posted: Tue Jul 28, 2020 04:56 PM

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
Posts: 3358
Joined: Fri Oct 07, 2005 08:20 PM
Re: Pregunta a los expertos en algoritmos
Posted: Tue Jul 28, 2020 06:29 PM

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

Continue the discussion