1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
bool bitn(int n, int pos) {
return (n >> pos)&1;
}
vector<int> sousEnsemble(const vector<int> &ens, int n) {
int rem, k=1,binary=0;
while(n!=0){
rem=n%2;
n=n/2;
binary=binary+rem*k;
k=k*10;
}
vector<int> sousens;
for(int i=0;i<ens.size();i++){
if(bitn(binary,i)){
sousens[i]=ens[i];
}
else
i++;
}
if(n==0)
sousens.clear();
return sousens;
}
void testSousEnsemble() {
cout << "Start tests: sous ensemble" << endl;
ASSERT(egal(sousEnsemble({1,2,3},0),{}));
ASSERT(egal(sousEnsemble({1,2,3},1),{1}));
ASSERT(egal(sousEnsemble({1,2,3},2),{2})); // binary 10
ASSERT(egal(sousEnsemble({1,2,3},3),{1,2})); // binary 11
ASSERT(egal(sousEnsemble({1,2,3},4),{3})); // binary 100
ASSERT(egal(sousEnsemble({1,2,3},5),{1,3})); // binary 101
ASSERT(egal(sousEnsemble({1,2,3},6),{2,3})); // binary 110
ASSERT(egal(sousEnsemble({1,2,3},7),{1,2,3})); // binary 111
cout << "Fin tests : sous ensemble" << endl;
}
| |