Share 78. subsets c++,

redelephant203

New member
#C ++, #Subsets, #C ++, #Subsets C ++ ### Tập hợp con trong C ++

Một tập hợp con của một bộ là một tập hợp được chứa trong tập hợp ban đầu.Nói cách khác, một tập hợp con là một tập hợp có các yếu tố cũng là các yếu tố của bộ gốc.

Ví dụ: tập {1, 2, 3} là một tập hợp con của tập {1, 2, 3, 4, 5}.

Chúng ta có thể viết mã sau để tìm tất cả các tập hợp con của một bộ:

`` `C ++
#include <Istream>
#include <Vector>

sử dụng không gian tên STD;

// Hàm này trả về tất cả các tập hợp con của một bộ.
Vector <Vector <Int>> getSubSets (vector <tint> set) {
// Số lượng tập con của một tập hợp với n phần tử là 2^n.
int numsubsets = pow (2, set.size ());

// Tạo một vectơ để lưu trữ tất cả các tập hợp con.
Vector <Vector <int >> tập hợp con (numsubsets);

// lặp lại tất cả các kết hợp có thể của bit.
for (int i = 0; i <numsubsets; i ++) {
// Tạo một vectơ để lưu trữ các yếu tố của tập hợp con hiện tại.
Vector <Int> tập hợp con;

// lặp lại tất cả các yếu tố của tập hợp.
for (int j = 0; j <set.size (); j ++) {
// Nếu bit thứ j của kết hợp hiện tại được đặt, thì thêm phần tử thứ j của tập hợp vào tập hợp con.
if ((i >> j) & 1) {
tập hợp con.push_back (đặt [j]);
}
}

// Thêm tập hợp con hiện tại vào vectơ của các tập hợp con.
tập hợp con = tập hợp con;
}

// Trả lại vectơ của các tập hợp con.
trả lại tập hợp con;
}

int main () {
// Tạo một bộ.
vector <int> set = {1, 2, 3};

// Nhận tất cả các tập hợp con của bộ.
Vector <Vector <int >> tập hợp con = getSubsets (set);

// In tất cả các tập hợp con.
for (int i = 0; i <subsets.size (); i ++) {
cout << "{" << endl;
for (int j = 0; j <subsets .size (); j ++) {
cout << tập hợp con [j] << "";
}
cout << "}" << endl;
}

trả lại 0;
}
`` `

Mã này sẽ in đầu ra sau:

`` `
{
1
2
3
1, 2
1, 3
2, 3
1, 2, 3
}
`` `

### hashtags

* #C ++
* #tập hợp con
* #sets
* #cấu trúc dữ liệu
* #algorithms
=======================================
#C++, #Subsets, #C++, #Subsets c++ ### Subsets in C++

A subset of a set is a set that is contained within the original set. In other words, a subset is a set whose elements are all also elements of the original set.

For example, the set {1, 2, 3} is a subset of the set {1, 2, 3, 4, 5}.

We can write the following code to find all the subsets of a set:

```c++
#include <iostream>
#include <vector>

using namespace std;

// This function returns all the subsets of a set.
vector<vector<int>> getSubsets(vector<int> set) {
// The number of subsets of a set with n elements is 2^n.
int numSubsets = pow(2, set.size());

// Create a vector to store all the subsets.
vector<vector<int>> subsets(numSubsets);

// Iterate over all the possible combinations of bits.
for (int i = 0; i < numSubsets; i++) {
// Create a vector to store the elements of the current subset.
vector<int> subset;

// Iterate over all the elements of the set.
for (int j = 0; j < set.size(); j++) {
// If the jth bit of the current combination is set, then add the jth element of the set to the subset.
if ((i >> j) & 1) {
subset.push_back(set[j]);
}
}

// Add the current subset to the vector of subsets.
subsets = subset;
}

// Return the vector of subsets.
return subsets;
}

int main() {
// Create a set.
vector<int> set = {1, 2, 3};

// Get all the subsets of the set.
vector<vector<int>> subsets = getSubsets(set);

// Print all the subsets.
for (int i = 0; i < subsets.size(); i++) {
cout << "{" << endl;
for (int j = 0; j < subsets.size(); j++) {
cout << subsets[j] << " ";
}
cout << "}" << endl;
}

return 0;
}
```

This code will print the following output:

```
{
1
2
3
1, 2
1, 3
2, 3
1, 2, 3
}
```

### Hashtags

* #C++
* #Subsets
* #sets
* #data structures
* #algorithms
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top