Pregunta a los expertos en algoritmos

Pregunta a los expertos en algoritmos

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

Re: Pregunta a los expertos en algoritmos

Postby nageswaragunupudi » Tue Jul 28, 2020 11:08 am

Code: Select all  Expand view  RUN
#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
nageswaragunupudi
 
Posts: 10662
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Pregunta a los expertos en algoritmos

Postby Armando » Tue Jul 28, 2020 2: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
User avatar
Armando
 
Posts: 3231
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México

Re: Pregunta a los expertos en algoritmos

Postby Armando » Tue Jul 28, 2020 3: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
User avatar
Armando
 
Posts: 3231
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México

Re: Pregunta a los expertos en algoritmos

Postby nageswaragunupudi » Tue Jul 28, 2020 4: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
User avatar
nageswaragunupudi
 
Posts: 10662
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Pregunta a los expertos en algoritmos

Postby Armando » Tue Jul 28, 2020 6: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
User avatar
Armando
 
Posts: 3231
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 63 guests