Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n;
- long long S;
- bool need = true;
- int values[24] = {};
- bool signs[24] = {};
- void recours(int index, bool sign){
- if (need){
- signs[index] = sign;
- if (index == n - 1 ){
- long long sum = 0;
- for(int i = 0; i < n; i++){
- if (signs[i])
- sum += values[i];
- else
- sum -= values[i];
- }
- if (sum == S){
- need = false;
- for(int i = 0; i < n; i++){
- if (i == 0){
- cout << values[i];
- }
- else{
- if (signs[i])
- cout << "+" << values[i];
- else
- cout << "-" << values[i];
- }
- }
- cout << "=" << S;
- }
- }
- else{
- recours(index+1, 0);
- recours(index+1, 1);
- }
- }
- }
- int main(){
- cin >> n;
- cin >> S;
- for(int i = 0; i < n; i++){
- cin >> values[i];
- }
- recours(0, 1);
- if (need)
- cout << "No solution";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement