用Java程序实现集合的运算(特别是增加 删除 修改 查询 交集 并集 等等幂集的快速运算)

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Test1<T> {
    Set<T> set = new HashSet<>();  //创建一个集合类

    Test1() {
    }             //?

    Test1(Set set) {     //可以用集合类的set初始化set
        this.set = set;
    }

    Test1(Test1 test1) {    //用自己的类类型来初始化自己

        this.set = test1.set;
    }


    Test1(T[] TS)          //任意的类型遍历初始化test1
    {
        for (T t : TS)
            set.add(t);
    }

    //输出:
    public void out()
    {
        System.out.println(set.toString());
    }

    public boolean insert(T element)  // 添加
    {
        return set.add(element);
    }

    public boolean delete(T element)   //删除
    {
        return set.remove(element);
    }

    public boolean searchElemnt(T element) {
        return set.contains(element);  //查找是否有这个子集
    }

    public boolean isSet(List<T> list) {
        Set set = new HashSet<>();
        for (T t : list) {
            if (!set.add(t)) {
                return false;
            }         //判断是否存在 Set集合中不能用相同的元素
        }
        return true;
    }

    public Test1<T> union(Test1<T> test1) {
        for (T t : test1.set) {                                   // 并集 把没有包含的T都包含进去
            this.set.add(t);
        }
        return this;
    }

    public Test1<T> gather(Test1<T> test1) {
        Test1<T> objectTest1 = new Test1<>();
        for (T t : set)   //自己本来的集合
        {
            if (test1.set.contains(t))   //如果有重复
            {
                objectTest1.set.add(t);
            }
        }
        return objectTest1;
    }

    public Test1<T> differ(Test1<T> test1) {
        for (T t : test1.set) {
            if (set.contains(t)) {
                set.remove(t);
            }
        }
        return this;
    }

    public Test1<T> oppsiteDiffer(Test1<T> test1) {
        for (T t : test1.set) {
            if (set.contains(t)) {                                         //  交叉并集
                set.remove(t);
            } else {
                set.add(t);
            }
        }
        return this;
    }

    public int size()              // 返回大小
    {
        int size = set.size();
        return size;
    }

    public boolean contain(Test1<T> test1) {
        if (set.containsAll(test1.set))   //判断是否包含;
        {
            return true;
        }
        return false;
    }


    //求幂集
    public Set sumSet(Set<Set<T>> sets,Set<T> set){
        //每次递归前先把当前集合加入到嵌套集合中set->sets
        sets.add(set);
        //遍历当前集合,依次减到一个元素,然后递归函数
        for(T t:set){
            Set<T> temp = new HashSet<>(set);
            temp.remove(t);
            sumSet(sets,temp);
        }
        return sets;
    }
}



前面的内容都很简单 但是幂集的部分运用到了递归的思想;意思就是 每一次算的时候都去掉一个数字 这个集合有四个数 那就有四次的循环运算:
在这里插入图片描述
这样的循环可以用递归的办法实现,这样以后每一次都会变化。具体你可以断点调试 会看的更明白,
在这里插入图片描述

总之你复制粘贴后断点调试就明白啦!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 C++ 实现集合运算操作的代码: ```c++ #include <iostream> #include <set> #include <vector> #include <algorithm> using namespace std; // 交集 set<int> intersection(set<int> s1, set<int> s2) { set<int> result; set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(result, result.begin())); return result; } // 并集 set<int> union_set(set<int> s1, set<int> s2) { set<int> result; set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(result, result.begin())); return result; } // 差集 set<int> difference(set<int> s1, set<int> s2) { set<int> result; set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(result, result.begin())); return result; } // 幂集 vector<set<int>> power_set(set<int> s) { vector<set<int>> result; int n = s.size(); for (int i = 0; i < (1 << n); i++) { set<int> subset; for (int j = 0; j < n; j++) { if (i & (1 << j)) { auto it = s.begin(); advance(it, j); subset.insert(*it); } } result.push_back(subset); } return result; } // 任意m元子集 vector<set<int>> m_subset(set<int> s, int m) { vector<int> indices(s.size()); iota(indices.begin(), indices.end(), 0); vector<set<int>> result; vector<int> combination(m, 0); int i = 0; while (i >= 0) { combination[i]++; if (combination[i] > s.size() - indices.size() + i) { i--; } else if (i == m - 1) { set<int> subset; for (int j = 0; j < m; j++) { auto it = s.begin(); advance(it, indices[combination[j]]); subset.insert(*it); } result.push_back(subset); } else { i++; indices[i] = indices[i - 1] + 1; combination[i] = combination[i - 1]; } } return result; } int main() { set<int> s1 = {1, 2, 3, 4}; set<int> s2 = {3, 4, 5, 6}; // 交集 set<int> intersection_set = intersection(s1, s2); cout << "Intersection: "; for (auto x: intersection_set) { cout << x << " "; } cout << endl; // 并集 set<int> union_set_ = union_set(s1, s2); cout << "Union: "; for (auto x: union_set_) { cout << x << " "; } cout << endl; // 差集 set<int> difference_set = difference(s1, s2); cout << "Difference: "; for (auto x: difference_set) { cout << x << " "; } cout << endl; // 幂集 vector<set<int>> power_set_ = power_set(s1); cout << "Power set:" << endl; for (auto subset: power_set_) { for (auto x: subset) { cout << x << " "; } cout << endl; } // 任意m元子集 vector<set<int>> m_subset_ = m_subset(s1, 3); cout << "3-subset:" << endl; for (auto subset: m_subset_) { for (auto x: subset) { cout << x << " "; } cout << endl; } return 0; } ``` 该代码使用 set 类型来表示集合,并使用 algorithm 库中的函数实现了交集并集、差集的操作。幂集的实现使用了位运算,任意 m 元子集的实现使用了组合数学中的技巧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值