473,378 Members | 1,438 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,378 developers and data experts.

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 373

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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.