Exemplo n.º 1
0
int main() {
	std::list<int> set{ 1, 2, 3 };
	std::vector<std::list<int>> ps = PowerSet(set);
	//display the test case 
	for (std::list<int> i : ps) {
		std::list<int>::const_iterator it = i.cbegin();
		if (i.size() == 0)
			std::cout << "{ }" << std::endl;
		else {
			std::cout << "{ ";
			while (it != i.end()) {
				std::cout << *it << " ";
				it++;
			}
			std::cout << "}";
			std::cout << std::endl;
		}
	}
	system("pause");
	return 0; 
}
Exemplo n.º 2
0
std::vector<std::list<T>> PowerSet(std::list<T>& set) {
	std::list<T> empty;
	std::vector<std::list<T>> ps, ps_temp;
	//return when the stop condition is reached
	if (set.size() == 0) {
		ps.push_back(empty);
		return ps;
	}

	T temp = set.back();
	set.pop_back();
	//call PowerSet recursively on a set with one less element
	ps_temp = PowerSet(set);

	ps.insert(ps.end(), ps_temp.begin(), ps_temp.end());
	for (std::list<T>& i : ps_temp) {
		i.push_back(temp);
	}
	ps.insert(ps.end(), ps_temp.begin(), ps_temp.end());
	return ps;
}
Exemplo n.º 3
0
int main() {
	std::set<int> S;
	std::vector< std::vector< std::set<int> > > PS;

	for (unsigned int n = 0; n < 6; n++) {
		S.insert(n + 1);
	}

	PowerSet(S, 2, PS);

	// for all subsets of size <0, ..., n>
	for (unsigned int n = 0; n < PS.size(); n++) {
		const std::vector<std::set<int> >& V = PS[n];

		printf("[n: %u]\n", n);

		// for all <k> subsets of size <n>
		for (unsigned int k = 0; k < V.size(); k++) {
			const std::set<int>& K = V[k];

			printf("\t[k: %u] = {", k + 1);

			// all elements of the <k>-th subset of size <n>
			for (std::set<int>::const_iterator it = K.begin(); it != K.end(); ++it) {
				std::set<int>::const_iterator iit = it;

				printf("%d", *it);

				if (++iit != K.end()) {
					printf(", ");
				}
			}

			printf("}\n");
		}
	}

	return 0;
}
Exemplo n.º 4
0
struct Array* AnimalFoodDep(const struct HashTable* _Table) {
    int i;
    int _SetSize = 0;
    struct Array* _Array = CreateArray(_Table->Size);
    struct Population* _Pop = NULL;
    struct HashItrCons* _Itr = HashCreateItrCons(_Table);
    struct FoodBase** _Temp = NULL;
    struct AnimalDep* _Dep = NULL;
    struct AnimalDep* _Search = NULL;
    struct FoodBase*** _Set = NULL;

    while(_Itr != NULL) {
        _Pop = _Itr->Node->Pair;
        for(i = 0; i < _Pop->EatsSize; ++i) {
            if((_Dep = BinarySearch(_Pop->Eats[i], _Array->Table, _Array->Size, AnDepArrayCmp)) == NULL) {
                _Dep = (struct AnimalDep*) malloc(sizeof(struct AnimalDep));
                _Temp = calloc(2, sizeof(struct FoodBase*));
                _Temp[0] = _Pop->Eats[i];
                _Temp[1] = NULL;
                _Dep->Tbl = _Temp;
                _Dep->Animals = CreateArray(4);
                _Dep->Nutrition = 0;
                ArrayInsertSort_S(_Array, _Dep, AnDepArrayArrayCmp);
            }
            ArrayInsertSort_S(_Dep->Animals, _Pop, PopulationCmp);
        }
        if(_Pop->EatsSize == 0)
            goto loopend;
        _Temp = calloc(_Pop->EatsSize + 1, sizeof(struct FoodBase*));
        _Dep = (struct AnimalDep*) malloc(sizeof(struct AnimalDep));
        for(i = 0; i < _Pop->EatsSize; ++i) {
            _Temp[i] = _Pop->Eats[i];
        }
        _Temp[i] = NULL;
        _Dep->Nutrition = _Pop->Nutrition;
        _Dep->Tbl = _Temp;

        _Set = PowerSet(_Temp,_Pop->EatsSize);
        _SetSize = pow(2, _Pop->EatsSize);
        for(i = 1; i < _SetSize; ++i) {
            if((_Search = BinarySearch(_Set[i], _Array->Table, _Array->Size, FoodArrayAnDepArray)) != NULL)
                _Search->Nutrition += _Pop->Nutrition;
            free(_Set[i]);
        }
        free(_Set[0]);
        free(_Set[_SetSize]);
        free(_Set);
        if((_Search = BinarySearch(_Dep, _Array->Table, _Array->Size, AnDepArrayArrayCmp)) != NULL) {
            ArrayInsertSort_S(_Search->Animals, _Pop, PopulationCmp);
            free(_Dep);
            free(_Temp);
            goto loopend;
        }
        _Dep->Animals = CreateArray(4);
        ArrayInsertSort_S(_Dep->Animals, _Pop, PopulationCmp);
        ArrayInsertSort_S(_Array, _Dep, AnDepArrayArrayCmp);
loopend:
        _Itr = HashNextCons(_Table, _Itr);
    }
    HashDeleteItrCons(_Itr);
    if(_Array->Size > 0) {
        _Array->Table = realloc(_Array->Table, _Array->Size * sizeof(struct FoodBase*));
        _Array->TblSize = _Array->Size;
    }
    return _Array;
}
Exemplo n.º 5
0
Arquivo: main.cpp Projeto: CCJY/coliru
int main() {
	// Read the input into the text vector
	std::ifstream inputFile("input.txt");
	std::vector<std::string> text;
	std::copy(std::istream_iterator<std::string>(inputFile),
		std::istream_iterator<std::string>(),
		std::back_inserter(text));

	if (text.size() == 0) {
		std::cout << "Input file could not be read";
		return -1;
	}

	// Now we no longer need the first number so discard it
	text.erase(std::begin(text));

	// Open the output file
	std::ofstream output("output.txt");
	if (!output.is_open()) {
		std::cout << "Error opening output file!" << std::endl;
		return -1;
	}

	int caseIndex = 1;
	// For each test case
	for (unsigned i = 0; i < text.size();) {
		// The first three lines are the amount of food he wants
		const auto needs = Format(
			std::stoi(text[i]), std::stoi(text[i + 1]), std::stoi(text[i + 2])
		);

		// The next line is the amount of food we can pick from
		const int amountFood = std::stoi(text[i + 3]);

		// Get all the food into vectors
		std::vector<Format> foodOptions;
		for (int foods = 0; foods < amountFood * 3; foods += 3) {
			const auto currentNumberFood = Format(
				std::stoi(text[i + 4 + foods]),
				std::stoi(text[i + 5 + foods]),
				std::stoi(text[i + 6 + foods])
			);

			if (currentNumberFood > needs) {
				continue;
			}

			foodOptions.push_back(currentNumberFood);
		}

		if (PowerSet(foodOptions, needs)) {
			output << "Case #" << caseIndex << ": yes" << std::endl;
		}
		else {
			output << "Case #" << caseIndex << ": no" << std::endl;
		}

		caseIndex++;
		i += 4 + amountFood * 3;
	}

	output.close();
}