473,422 Members | 1,894 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,422 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 16951
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.