Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Задача 7. Да се напише функция на С++, която намира всички подмножества на дадено универсално множество, които съдържат елемент k (въведен от потребителя).*/
- void genElemKSet(int * uset, int * set, unsigned sz, int elem, unsigned curr);
- int main()
- {
- const unsigned sz = 5;
- int uarr[sz] = {1, 2, 3, 4, 5};
- char bitmaskA[] = "10110";
- int subset[sz];
- cout << "Genetate all subsets of the universal set, that have element with value 4: " << endl;
- genElemKSet(uarr, subset, sz, 4, 0);
- }
- void genElemKSet(int * uset, int * set, unsigned sz, int elem, unsigned curr){
- if(curr==sz) printArray(set, sz);
- else{
- unsigned ind = 0;
- for(unsigned i=0; i<sz; i++){
- if(uset[i] == elem) {
- ind = i;
- break;
- }
- }
- if(curr != ind){
- set[curr] = 0;
- genElemKSet(uset, set, sz, elem, curr+1);
- set[curr] = 1;
- genElemKSet(uset, set, sz, elem, curr+1);
- }
- else{
- set[ind] = 1;
- genElemKSet(uset, set, sz, elem, curr+1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement