I want to try markdown.
Just take a photoGo 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