|
|
вернуться в форумHow to find all subsets os set in C++? How to find all subsets os set in C++? Re: How to find all subsets os set in C++? #include<iostream> #include<cmath> using namespace std; int main(){ int n,a[20],delta=1000000000; cin>>n; for(int i=0;i<n;i++) cin>>a[i]; int s1=0,s2=0; int t = 1<<n; for(int i=0;i<t;i++){ s1=0; s2=0; for(int j=0;j<n;j++){ if( ( (1<<j) & (i) ) == 0 ) s1+=a[j]; else s2+=a[j]; } if(abs(s1-s2) < delta) delta = abs(s1-s2);
} cout<<delta<<endl; return 0; } Re: How to find all subsets os set in C++? And if you write: int t = 1 << (n - 1); // instead of 1 << n it will be 2 times faster :) |
|
|