Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6. void subset(int* set) {
  7. int n = 3; // for example three size of data
  8. cout << "-" << endl;;
  9. for (int i = 1; i < (1 << n); i++) {
  10.  
  11. for (int j = 0; j < n; j++) {
  12. if (i & (1 << j))
  13. cout << set[j] << ",";
  14. }
  15. cout << endl;
  16. }
  17.  
  18. }
  19. int main() {
  20. int set[3] = { 1,2,3 };
  21. subset(set);
  22. return 0;
  23.  
  24. }
  25.  
  26. #include <iostream>
  27. #include <math.h>
  28. using namespace std;
  29.  
  30.  
  31. void printPowerSet(char *set, int set_size)
  32. {
  33. /*set_size of power set of a set with set_size
  34. n is (2**n -1)*/
  35. unsigned int pow_set_size = pow(2, set_size);
  36. int counter, j;
  37.  
  38. /*Run from counter 000..0 to 111..1*/
  39. for(counter = 0; counter < pow_set_size; counter++)
  40. {
  41. for(j = 0; j < set_size; j++)
  42. {
  43. /* Check if jth bit in the counter is set
  44. If set then print jth element from set */
  45. if(counter & (1 << j))
  46. cout << set[j];
  47. }
  48. cout << endl;
  49. }
  50. }
  51.  
  52.  
  53. /*Driver code*/
  54. int main()
  55. {
  56.  
  57. char set[] = {'a','b','c'};
  58. printPowerSet(set, 3);
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement