ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1005. Stone Pile

How to find all subsets os set in C++?
Posted by Guti Alberto MOSKVA 11 Jun 2010 16:24
How to find all subsets os set in C++?
Re: How to find all subsets os set in C++?
Posted by Baurzhan 11 Jun 2010 19:00
#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++?
Posted by Moonstone 11 Jun 2010 21:08
And if you write:
int t = 1 << (n - 1); // instead of 1 << n
it will be 2 times faster :)