fork download
  1. #include <iostream> // KNAPSACK 0-1 DANILIN
  2. using namespace std; int main()
  3. { setlocale (LC_ALL, "RUS");
  4. srand(time(NULL));
  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. cout << "# Кол Цена" << endl;
  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. }
  16. for (i=0; i<a; i++) { q[i]=0; d[i]=0;}
  17. cout << endl;
  18. cout << "Мх Кол Ст-ть Шифр" << endl;
  19. cout << "Mx Amo Price Cipher" << endl << endl;
  20. for (h = a-1; h>(a-1)/2; h--)
  21. { dec=h; while (dec > 0)
  22. { string s(""); s += '0'+dec%2;
  23. e[h] = s + e[h]; dec/=2;
  24. }
  25. if (e[h] == "") {e[h] = "0";}
  26. e[h]= e[h].substr(1, e[h].size()-1);
  27.  
  28. for (k=0; k<n; k++)
  29. { j[k] = atoi((char*)(e[h].substr(k,1)).c_str());
  30. q[h]=q[h]+L[k]*j[k]*C[k];
  31. d[h]=d[h]+L[k]*j[k];
  32. }
  33. if (d[h] <= G)
  34. cout << G << " " << d[h] << " " << q[h] << " " << e[h] << endl;
  35. } cout << endl;
  36. max=0; m=1;
  37. for (i=0; i<a; i++)
  38. { if (d[i]<=G && q[i]>max){ max=q[i]; m=i;}
  39. }
  40. cout << "Мх Ст-ть Шифр" << endl;
  41. cout << "Mx Price Cipher" << endl << endl;
  42. cout << d[m] << " " << q[m] << " " << e[m] << endl << endl;}
  43. system("pause");
  44. }
Success #stdin #stdout #stderr 0.01s 5292KB
stdin
Standard input is empty
stdout
#  Кол  Цена
#  Amo Price

1   2   15
2   1   11
3   2   14
4   1   11
5   1   15
6   2   18
7   1   14

Мх Кол Ст-ть Шифр
Mx Amo Price Cipher

5  5  69  1110000
5  5  67  1101100
5  5  66  1101001
5  4  52  1101000
5  5  70  1100101
5  4  56  1100100
5  5  77  1100010
5  4  55  1100001
5  3  41  1100000
5  5  69  1011000
5  5  73  1010100
5  5  72  1010001
5  4  58  1010000
5  5  70  1001101
5  4  56  1001100
5  5  77  1001010
5  4  55  1001001
5  3  41  1001000
5  5  81  1000110
5  4  59  1000101
5  3  45  1000100
5  5  80  1000011
5  4  66  1000010
5  3  44  1000001
5  2  30  1000000
5  5  65  0111100
5  5  64  0111001
5  4  50  0111000
5  5  68  0110101
5  4  54  0110100
5  5  75  0110010
5  4  53  0110001
5  3  39  0110000
5  5  73  0101110
5  4  51  0101101
5  3  37  0101100
5  5  72  0101011
5  4  58  0101010
5  3  36  0101001
5  2  22  0101000
5  5  76  0100111
5  4  62  0100110
5  3  40  0100101
5  2  26  0100100
5  4  61  0100011
5  3  47  0100010
5  2  25  0100001
5  1  11  0100000
5  5  68  0011101
5  4  54  0011100
5  5  75  0011010
5  4  53  0011001
5  3  39  0011000
5  5  79  0010110
5  4  57  0010101
5  3  43  0010100
5  5  78  0010011
5  4  64  0010010
5  3  42  0010001
5  2  28  0010000
5  5  76  0001111
5  4  62  0001110
5  3  40  0001101
5  2  26  0001100
5  4  61  0001011
5  3  47  0001010
5  2  25  0001001
5  1  11  0001000
5  4  65  0000111
5  3  51  0000110
5  2  29  0000101
5  1  15  0000100
5  3  50  0000011
5  2  36  0000010
5  1  14  0000001
5  0  0  0000000

Мх Ст-ть Шифр
Mx Price Cipher

5  81  1000110

stderr
sh: 1: pause: not found