473,461 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Knapsack 0-1 C++ binary & WE

24 16bit
Knapsack 0-1 C++ binary & 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

Feature of algorithm:
converts whole into a string
and converts string into a whole
what will help in other programs

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. #include <iostream> // KNAPSACK 0-1 DANILIN
  2. using namespace std; int main()
  3. { setlocale (LC_ALL, "RUS");
  4.   srand(time(NULL)); // rextester.com/VCBSQ91995
  5.  
  6. { int n=7; int G=5; int a=2;  
  7.   int dec, i, h, k, max, m; 
  8.   for (i=0; i<n; i++) a=2*a; string e[a]; // 2^n 
  9.   int L[n], C[n], j[n], q[a], d[a];  
  10.  
  11. cout << "#  Amo Price" << endl << endl; 
  12. for (i=0; i<n; i++)
  13. { L[i]=1+(rand() % 3); C[i]=10+(rand() % 9); j[i]=0;
  14.   cout << i+1 << "   " << L[i] << "   " << C[i] << endl; 
  15. for (i=0; i<a; i++) { q[i]=0; d[i]=0;}
  16. cout << endl; 
  17.  
  18. cout << "Mx Amo Price Chifer" << endl << endl; 
  19. for (h = a-1; h>(a-1)/2; h--)
  20.   { dec=h; while (dec > 0)
  21.       { string s(""); s += '0'+dec%2;   
  22.         e[h] = s + e[h]; dec/=2; 
  23.       }
  24. if (e[h] == "") {e[h] = "0";}
  25. e[h]= e[h].substr(1, e[h].size()-1);
  26.  
  27. for (k=0; k<n; k++)
  28. { j[k] = atoi((char*)(e[h].substr(k,1)).c_str()); 
  29.   q[h]=q[h]+L[k]*j[k]*C[k];
  30.   d[h]=d[h]+L[k]*j[k];
  31. }
  32.  
  33. if (d[h] <= G)
  34. cout << G << "  " << d[h] << "  " << q[h] << "  " << e[h] << endl; 
  35. } cout << endl;
  36.  
  37. max=0; m=1;
  38. for (i=0; i<a; i++)
  39. { if (d[i]<=G && q[i]>max){ max=q[i]; m=i;}
  40. }
  41. cout << "Mx Price Cipher" << endl << endl; 
  42. cout << d[m] << "  " << q[m] << "  " << e[m] << endl << endl;}
  43. system("pause");
  44. }
Feb 6 '23 #1
0 379

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

Similar topics

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?
0
by: gauravkhanna | last post by:
Hi All I need some help for the below problem: Scenario We need to send large binary files (audio file of about 10 MB or so) from the client machine (.Net Windows based application, located...
7
by: Sascha Schmidt | last post by:
Hello ! I have a little problem accessing a Visual Foxpro-database using ODBC (Driver 6.00.8167) (using System.Data.Odbc). Reading a column, defined in the Foxpro table definition as a kind of...
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 ) ....
8
by: Tehcuod | last post by:
Ok, so, for those of you that are unfamiliar with the 0-1 Knapsack problem, I have to write an to take the weights and corresponding values of 10 items, and find which items to put in a knapsack that...
1
by: DANILIN | last post by:
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...
0
by: DANILIN | last post by:
Knapsack 0-1 Python 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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.