Created
June 2, 2017 22:44
-
-
Save adler3d/aedad1b50fd8e70a208461c8c490c15b to your computer and use it in GitHub Desktop.
started from this: http://www.gamedev.ru/flame/forum/?id=226776
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://ideone.com/H2uNwE | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
vector<vector<int>> out; | |
void f(int N,int L,const vector<int>&prefix){ | |
auto arr=prefix; | |
for(auto i=arr.empty()?0:arr.back()+1;i<N;i++){ | |
arr.push_back(i); | |
if(L==arr.size())out.push_back(arr); | |
f(N,L,arr); | |
arr.pop_back(); | |
} | |
} | |
int sum(const vector<int>&arr,const vector<int>&ids){int v=0;for(size_t i=0;i<ids.size();i++)v+=arr[ids[i]];return v;} | |
bool check(const vector<int>&inp){ | |
vector<int> e; | |
int n=inp.size(); | |
for(int i=0;i<n;i++)f(n,i+1,e); | |
for(int i=0;i<out.size();i++){ | |
auto&arr=out[i]; | |
if(0==sum(inp,arr)){for(int i=0;i<arr.size();i++)cout<<inp[arr[i]]<<" ";cout<<endl;return true;} | |
} | |
return false; | |
} | |
int main() { | |
int inp_arr[]={2,-7,4,8,1}; int n=5; | |
vector<int> inp(inp_arr,inp_arr+n); | |
cout<<(check(inp)?"true":"false"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment