01 backpack algorithm implemented using coffee and socket. Io

Source: Internet
Author: User
Tags emit
Let me explain why I wrote this first.
  • When the program is too hard, really, for a long time, really do not have a sense of accomplishment to move bricks, good people can build a building (body can also be good ), we don't know which day a bear child will get rid of the hard drive.

  • This semester, I have never been at the forefront ...... Khan, I still want to eat some food in the future, but I have never had a rest this semester, but I am busy with a lot of things that many people may think are boring.

  • This is because there are too many things to talk about in this semester, which may lead to a lot of delays, disappoint the mentor, and feel inferior.

  • Because many of them are wonderful ideas about homework, it is not a job if you don't confuse it. I hope you will criticize it a lot.

  • Probably because of which article can solve the problem.

  • I want to try markdown.

    Just take a photo

    Go to the background implementation section of the subject:

    IO = require "socket. Io"
    HTTP = require "http"
    FS = require "Fs"
    Express = require "express"
    Mime = require "mime"
    APP = Express ()

    Server = http. createserver app
    Server. Listen 8080
    Console. log "Listening 8080"

    App. Get "/", (req, Res)->

    path = "#{__dirname}/console.html"res.writeHead 200,"Content-Type":mime.lookup(path)res.end fs.readFileSync path

    App. Get "/jquery. Min. js", (req, Res)->

    path = "#{__dirname}/jquery.min.js"res.writeHead 200,"Content-Type":mime.lookup(path)res.end fs.readFileSync path

    App. Get "/Bootstrap. Min. js", (req, Res)->

    path = "#{__dirname}/bootstrap.min.js"res.writeHead 200,"Content-Type":mime.lookup(path)res.end fs.readFileSync path

    App. Get "/bootstrap.min.css", (req, Res)->

    path = "#{__dirname}/bootstrap.min.css"res.writeHead 200,"Content-Type":mime.lookup(path)res.end fs.readFileSync path

    Getcurrenttime =->
    D = new date ()
    Return "# {d. getfullyear ()}-# {d. getmonth () + 1}-# {d. getdate () }# {d. gethours () }:# {d. getminutes () }:# {d. getseconds ()}"

    Class dynamicpack

    Pack :( data)-> C = [] I = 0 J = 0 while I <data. m + 1 C [I] = [] C [I] [0] = 0 I ++ while j <data. n + 1 C [0] [J] = 0 J ++ I = 1 while I <data. m + 1 j = 1 while j <data. n + 1 if data. W [I-1] <= J if C [I-1] [J] <C [I-1] [j-data.w] + data. V [I-1] C [I] [J] = C [I-1] [j-data.w [I-1] + data. V [I-1] else C [I] [J] = C [I-1] [J] else C [I] [J] = C [I-1] [J] J ++ I ++ return C; print :( C, data)-> X = [] I = data. m n = data. n str = "" # console. log C [I] [m] While I> 0 if C [I] [N]> C [I-1] [N] X [I-1] = 1 N-= data. W [I-1] else X [I-1] = 0 I -- I = 0 count = 0 while I <data. M count + = x [I] * data. V [I] STR + = (I + 1) + "," If X [I]! = 0 I ++ return STR + "total value # {count }"

    Class knappack

    pack : (data)->    @v = data.v    @w = data.w    @m = data.m    @n = data.n    @cw = 0    @cv = 0    @put = []    @bestp = 0    temp_order = 0;    temp = 0    perp = []    i=0    while i<@m        perp[i] = @v[i]/@w[i]         @put[i] = 0;        i++    console.log perp    i=0    while i<@m        j=i+1        while j<@m            if perp[i]<perp[j]                temp = @v[i]                @v[i] = @v[j]                @v[j] = temp                temp = @w[i]                @w[i] = @w[j]                @w[j] = temp            j++        i++backtrack : (i)->    console.log i    @bound i    if i>@m        @bestp = @cv        return    if @[email protected][i]<[email protected]        @[email protected][i]        @[email protected][i]        @put[i]=1        @backtrack(i+1)        @[email protected][i]        @[email protected][i]    if @bound(i+1)>@bestp        @backtrack(i+1)bound :(i)->    leftw = @n - @cw    b = @cv    while i<[email protected] and @w[i]<=leftw        leftw -= @w[i]        b += @v[i]        i++    [email protected][i]/@w[i]*leftw if i<@m    return bprint :(data)->    @pack(data)    console.log @w    console.log @v    @backtrack(0)    console.log @put     return @bestp

    Dask = (MSG)->

    Answer = "" Data = JSON. parse msgconsole. log DATAD = new dynamicpack () console. log D. pack (data) Answer + = "Dynamic Planning, select item" + D. print D. pack (data), datareturn answer

    Kask = (MSG)->

    Answer = "" Data = JSON. parse msgconsole. Log Datak = new knappack () Answer + = "branch limit, optimal solution" + K. Print datareturn answer

    Io. Listen (server). On "connection", (socket)->

    socket.on "msg",(msg)->    ##console.log msg    socket.emit "msg",{time:getCurrentTime(),text:"calculating..."}    socket.emit "msg",{time:getCurrentTime(),text:dask(msg)}    socket.emit "msg",{time:getCurrentTime(),text:kask(msg)}    ##socket.broadcast.emit "msg",dataconsole.log "#{getCurrentTime()}:Connected"
    Front-end implementation:












    • Input example: {"N": 10, "M": 3, "W": [3, 4, 5], "V": [4, 5, 6]} where N is the backpack capacity and m is the number of items


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.