Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6. long long S;
  7. bool need = true;
  8.  
  9. int values[24] = {};
  10. bool signs[24] = {};
  11.  
  12. void recours(int index, bool sign){
  13.     if (need){
  14.         signs[index] = sign;
  15.         if (index == n - 1 ){
  16.             long long sum = 0;
  17.             for(int i = 0; i < n; i++){
  18.                 if (signs[i])
  19.                     sum += values[i];
  20.                 else
  21.                     sum -= values[i];
  22.             }
  23.             if (sum == S){
  24.                 need = false;
  25.                 for(int i = 0; i < n; i++){
  26.                     if (i  == 0){
  27.                         cout << values[i];
  28.                     }
  29.                     else{
  30.                         if (signs[i])
  31.                             cout << "+" << values[i];
  32.                         else
  33.                             cout << "-" << values[i];
  34.                     }
  35.                 }
  36.                 cout << "=" << S;
  37.             }
  38.  
  39.         }
  40.         else{
  41.             recours(index+1, 0);
  42.             recours(index+1, 1);
  43.         }
  44.     }
  45. }
  46.  
  47.  
  48. int main(){
  49.     cin >> n;
  50.     cin >> S;
  51.  
  52.     for(int i = 0; i < n; i++){
  53.         cin >> values[i];
  54.     }
  55.  
  56.     recours(0, 1);
  57.     if (need)
  58.         cout << "No solution";
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement