473,289 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,289 developers and data experts.

Knapsack 0-1 C# binary & rosettacode & WE

24 16bit
Knapsack 0-1 C# binary & rosettacode & WE

Classic Knapsack problem is solved in many ways

My newest program synthesizes all ciphers from 0 & 1
adding an extra register and 0 remain on left in cipher

Number of comparisons decreases from N! to 2^N
for example N=10 & N!=3628800 >> 2^N=1024

Random values origin are automatically assigned
quantity and quality and integral of value is obtained
and in general: integral of quantity and quality
Expand|Select|Wrap|Line Numbers
  1. using System;using System.Text;        // KNAPSACK 0-1 DANILIN     
  2. namespace Knapsack { class Program { static void Main()
  3.  
  4. { int n=5; int G=5; int u=n+1; int a=Convert.ToInt32(Math.Pow(2,u)); 
  5. int[] L = new int[n]; int[] C = new int[n]; int[] j = new int[n]; 
  6. int[] q = new int[a]; int[] S = new int[a]; int[] d = new int[a]; 
  7. int dec; int i; string[] e = new string[a]; 
  8. int h; int k; int max; int m; Random rand = new Random();
  9.  
  10. for (i=0; i<n; i++) // rextester.com/OIALC94208
  11. {L[i]=1+rand.Next(3); C[i]=10+rand.Next(9);
  12. Console.Write(i+1); Console.Write("   ");
  13. Console.Write(L[i]); Console.Write("   "); 
  14. Console.Write(C[i]);Console.WriteLine(); 
  15. } Console.WriteLine();
  16.  
  17. for (h = a-1; h>(a-1)/2; h--) 
  18. { dec=h; while (dec > 0)
  19. { e[h] = dec % 2 + e[h]; dec/=2; }
  20. if (e[h] == "") {e[h] = "0";}
  21. e[h]=e[h].Substring(1,e[h].Length-1);
  22.  
  23. for (k=0; k<n; k++)
  24. {j[k]=Convert.ToInt32(e[h].Substring(k,1));
  25.  
  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. { Console.Write(G);  Console.Write("  "); 
  31.  Console.Write(d[h]); Console.Write("  "); 
  32.  Console.Write(q[h]); Console.Write("  "); 
  33.  Console.WriteLine(e[h]);} 
  34. } Console.WriteLine();
  35.  
  36. max=0; m=1;
  37. for (i=0; i<a; i++)
  38. { if (d[i]<=G && q[i]>max)
  39. { max=q[i]; m=i;}}
  40.  
  41. Console.Write(d[m]); Console.Write("  "); 
  42. Console.Write(q[m]); Console.Write("  "); 
  43. Console.WriteLine (e[m]);}
  44. }}
Main thing is very brief and clear to even all

Results is reduced manually:
Expand|Select|Wrap|Line Numbers
  1. # Mass Cost
  2. 1 2 12
  3. 2 3 17
  4. 3 1 14
  5. 4 3 17
  6. 5 1 13
  7. Chifer Mass Cost 
  8. 11000 5 5 75
  9. 01001 5 4 64
  10. 00111 5 5 78 !!!
  11. 00110 5 4 65
  12. 00101 5 2 27
  13. Mass MAX Chifer
  14. 5 78 00111
Jun 1 '22 #1
1 16938
DANILIN
24 16bit
Python version of Knapsack was offered by me
in branch
https://bytes.com/topic/python/insights/

however it has not been published yet

Please promote my Python theme
or I will post algorithm in this topic for a month

Plus I created simplest version for Excel
Jun 22 '22 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Steve | last post by:
Hi, i know this is an old question (sorry) but its a different problem, i need to write a binary file as follows 00000011 00000000 00000000 00000101 00000000 11111111
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
20
by: William | last post by:
Original question: "Give a one-line C expression to test whether a number is a power of 2. " Answer: if (x && !(x & (x-1)) == 0) My question: Why does this expression work?
17
by: orekinbck | last post by:
Hi There Say I want to check if object1.Property1 is equal to a value, but object1 could be null. At the moment I have code like this: if (object1 != null) { if (object1.Property ==...
11
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved...
4
by: muthu | last post by:
In the following code it gives the error "error: invalid operands to binary &" Why it is happening #include <signal.h> #include <errno.h> #define SIGBAD(signo) ((signo) <= 0 || (signo) >=...
4
by: joe | last post by:
how to resize an upload image and then change to binary & insert to db
1
by: Richard Eich | last post by:
gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-3) source snippet: .... int i = 17 ; if ( 0x03 & i ) ....
3
by: asclearuc | last post by:
Hello Is it possible to use map<string&, string&>? Why I need it. I have a large amount of data obtained from XML file. I should do processing of this data. The processing takes many...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.