fork download
  1. var n=12; G=2; a = Math.pow(2,n+1); // KNAPSACKj.js
  2. var dec, i, h, k, max, m, s;
  3. var L=[n], C=[n], j=[n], q=[a], d=[a]; e=[a];
  4. print("# Kol Cena")
  5. print("# Amo Price")
  6. for (i=0; i<n; i++)
  7. { L[i]=1+Math.floor(Math.random()*3)
  8. C[i]=10+Math.floor(Math.random()*9); j[i]=0;
  9. print( (i+1) +" "+ L[i] +" "+ C[i])
  10. }
  11.  
  12. for (i=0; i<a; i++) { q[i]=0; d[i]=0;}
  13. print("")
  14. print("Mx Kol St-st Schifr")
  15. print("Mx Amo Price Cipher")
  16. for (h = a-1; h>(a-1)/2; h--)
  17. { dec=h; e[h]=""
  18. while (dec > 0)
  19. { s = Math.floor(dec % 2);
  20. e[h] = s + e[h]; dec = Math.floor(dec/2);
  21. }
  22. if (e[h] == "") {e[h] = "0";}
  23. e[h]= e[h].substr(1, e[h].length-1);
  24. for (k=0; k<n; k++)
  25. { j[k] = Number(e[h].substr(k,1));
  26. q[h]=q[h]+L[k]*j[k]*C[k];
  27. d[h]=d[h]+L[k]*j[k];
  28. }
  29. if (d[h] <= G)
  30. print(G +" "+ d[h] +" "+ q[h] +" "+ e[h])
  31. } print("")
  32. max=0; m=1;
  33. for (i=0; i<a; i++)
  34. { if (d[i]<=G && q[i]>max){ max=q[i]; m=i;}
  35. }
  36. print(d[m] +" "+ q[m] +" "+ e[m])
  37. print("Mx St-st Schifr")
  38. print("Mx Price Cipher")
  39.  
  40.  
Success #stdin #stdout 0.08s 24600KB
stdin
Standard input is empty
stdout
#  Kol Cena
#  Amo Price
1 1 16
2 3 11
3 3 14
4 1 11
5 3 12
6 1 14
7 1 17
8 2 14
9 1 11
10 2 16
11 3 11
12 3 15

Mx Kol St-st Schifr
Mx Amo Price Cipher
2 2 27 100100000000
2 2 30 100001000000
2 2 33 100000100000
2 2 27 100000001000
2 1 16 100000000000
2 2 25 000101000000
2 2 28 000100100000
2 2 22 000100001000
2 1 11 000100000000
2 2 31 000001100000
2 2 25 000001001000
2 1 14 000001000000
2 2 28 000000101000
2 1 17 000000100000
2 2 28 000000010000
2 1 11 000000001000
2 2 32 000000000100
2 0 0 000000000000

2 33 100000100000
Mx St-st Schifr
Mx Price Cipher