Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. bool toBinary(int a, char * res, int len, int posToBePresent, unsigned numOnes) {
  6.     int b, i, j=0;
  7.     bool isPresent=false;
  8.     unsigned numOnesInNum=0;
  9.     for(i=0;i<len;i++){
  10.         b=(a>>i)&1;
  11.         res[j++] = (char)(b+48);
  12.         if(i==posToBePresent && b+48=='1'){
  13.             isPresent=true;
  14.         }
  15.         if(b+48=='1'){
  16.             numOnesInNum++;
  17.         }
  18.     }
  19.     res[j] = '\0';
  20.     return isPresent && numOnesInNum==numOnes;
  21. }
  22.  
  23. int printPermutation(int *arr, char *mask, unsigned size){
  24.     char *p = mask;
  25.     for(unsigned i=0; i<size; i++){
  26.         if(*p=='1'){
  27.             cout<<arr[i];
  28.         }
  29.         p++;
  30.     }
  31.     cout<<endl;
  32.     return 0;
  33. }
  34.  
  35. int generatePermutations(int *arr, unsigned size, unsigned sizeOfPermutation, unsigned posToBePresent){
  36.     int power = pow(2, size);
  37.     for(unsigned i=0; i<power; i++){
  38.         char *mask = new char[33];
  39.         if(toBinary(i, mask, size, posToBePresent, sizeOfPermutation)){
  40.             cout<<mask<<endl;
  41.             //printPermutation(arr, mask, sizeOfPermutation);
  42.         }
  43.         mask = NULL;
  44.         delete[] mask;
  45.     }
  46.     return 0;
  47. }
  48.  
  49. int main() {
  50.     //power
  51.     unsigned k;
  52.     cin>>k;
  53.  
  54.     int *array = new int[k];
  55.     for(unsigned i=0; i<k; i++){
  56.         cin>>array[i];
  57.     }
  58.  
  59.     //power of subsets
  60.     unsigned n;
  61.     cin>>n;
  62.  
  63.     //position of one
  64.     unsigned p;
  65.     cin>>p;
  66.  
  67.     generatePermutations(array, k, n, p-1);
  68.  
  69.     if(array!=NULL){
  70.         delete[] array;
  71.     }
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement