fork(6) download
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. /*
  9.   string name;
  10.   cout << "What is your name? ";
  11.   getline(cin, name);
  12.   cout << "Hello, " << name << "!\n";
  13. */
  14.  
  15.  
  16.  
  17. int Massiv[24] = {0}, //24 числа последовательности
  18. N, S, //N - количество чисел, S - суммарное число
  19. AllCombinations = 0,
  20. Combination = 0,
  21. Znak,
  22. Resultat, n;
  23.  
  24. N = 3;
  25. S = 13;
  26. /*
  27. cout << "Vvedite N:";
  28. cin >> N;
  29. cout << "Vvedite S:";
  30. cin >> S;
  31.  
  32.  
  33. cout << "Vvedite massiv chisel:\n";
  34. for(n = 0; n < N; n++)
  35. {
  36.   cout << "\n" << n << "-i element:";
  37.   cin >> Massiv[n];
  38.   cout << "\nMassiv[n] = " << n;
  39. }
  40. */
  41. Massiv[0] = 7;
  42. Massiv[1] = 3;
  43. Massiv[2] = 9;
  44. Massiv[3] = 0;
  45.  
  46.  
  47. AllCombinations = pow(2, N-1);
  48. cout << "AllCombinations = " << AllCombinations << "\n";
  49.  
  50. for(Combination = 0; Combination < AllCombinations; Combination++)
  51. {
  52. Resultat = Massiv[0];
  53. //cout << "Combination = " << Combination << "\n"; //Debug
  54. for(Znak = N-1; Znak >= 0; Znak--)
  55. {
  56. //cout << "Znak = " << Znak << "\n"; //Debug
  57. if(Combination & (1<<(Znak-1)))
  58. {//-
  59. Resultat -= Massiv[N-Znak];
  60. //cout << "if = -" << "\n"; //Debug
  61. }
  62. else
  63. {//+
  64. Resultat += Massiv[N-Znak];
  65. //cout << "if = +" << "\n"; //Debug
  66. }
  67.  
  68. }
  69. cout << "\nResultat = " << Resultat << "\n";
  70. cout << "-----------------------------------------------------\n";
  71. if(Resultat == S)
  72. {
  73. cout << "Кодовая комбинация найдена!!!!!\n";
  74. cout << "Combination = " << Combination << "\n";
  75. //Вывод кодовой комбинации
  76. cout << Massiv[0];
  77. for(n = 1; n < N; n++)
  78. {
  79. if(Combination & (1<<(N-n-1)))
  80. {//-
  81. cout << "-";
  82. }
  83. else
  84. {//+
  85. cout << "+";
  86. }
  87. cout << Massiv[n];
  88. }
  89. cout << "=" << S;
  90. return 0;
  91. }
  92. }
  93.  
  94. cout << "Кодовая комбинация не найдена.:(";
  95. return 0;
  96. }
  97.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
AllCombinations = 4

Resultat = 19
-----------------------------------------------------

Resultat = 1
-----------------------------------------------------

Resultat = 13
-----------------------------------------------------
Кодовая комбинация найдена!!!!!
Combination = 2
7-3+9=13