Rosalind编程问题之集合操作。
题不难,但是格式处理繁琐。
Problem:
If A and B are sets, then their union A∪B is the set comprising any elements in either A or B; their intersection A∩B is the set of elements in both A and B; and their set difference A−B is the set of elements in A but not in B.
Furthermore, if A is a subset of another set U, then the set complement of A with respect to U is defined as the set Ac=U−A. See the Sample sections below for examples.
Given: A positive integer n (n≤20,000) and two subsets A and B of {1,2,…,n}.
Sample input:
10
{1, 2, 3, 4, 5}
{2, 8, 5, 10}
Return: Six sets: A∪B, A∩B, A−B, B−A, Ac, and Bc (where set complements are taken with respect to {1,2,…,n}).
Sample output:
{1, 2, 3, 4, 5, 8, 10}
{2, 5}
{1, 3, 4}
{8, 10}
{8, 9, 10, 6, 7}
{1, 3, 4, 6, 7, 9}
集合的交集,并集和补集是高中第一节课就学习的知识。Java语言对于构建集合有两类体系:collection单列集合和map双列集合。二者区别是前者每个元素只包含一个值,后者每个元素包含一个键值对(两个值)。本文使用了Set集合解题,Set集合基本的特点是添加的元素无序的,不重复,无索引。
主要知识点: 集合交并补处理核心公式:set.contains。
代码如下:
注意!!本文的代码需要在输入数据时手动去掉首尾括号。输出时手动更换 [ ] 括号为 { }。 否则都会被系统判定为错误。
例如本题示例数据:
10
{1, 2, 3, 4, 5}
{2, 8, 5, 10}
要以这个形式逐行输入:
10
1, 2, 3, 4, 5
2, 8, 5, 10
得到的答案是下述结果,需要手动更换[ ] 括号为 { }。
[1, 2, 3, 4, 5, 8, 10]
[2, 5]
[1, 3, 4]
[8, 10]
[6, 7, 8, 9, 10]
[1, 3, 4, 6, 7, 9]
public class Introduction_to_Set_Operations {
public static void main(String[] args) {
//1.初始化三个集合
Scanner sc = new Scanner(System.in);
System.out.println("请输入集合总元素数n:");
int n = sc.nextInt();
List total = new ArrayList<>();
for (int i = 1; i <= n; i++) {
total.add(i);
}
//构建n个元素组成的总集合
Scanner OA = new Scanner(System.in);
System.out.println("请输入集合A(去掉首位括号):");
String[] strnumA = OA.nextLine().split(", ");
Scanner OB = new Scanner(System.in);
System.out.println("请输入集合B(去掉首位括号):");
String[] strnumB = OB.nextLine().split(", ");
int[] numA = new int[strnumA.length];
for (int i = 0; i < strnumA.length; i++) {
numA[i] = Integer.parseInt(strnumA[i]);
}
int[] numB = new int[strnumB.length];
for (int i = 0; i < strnumB.length; i++) {
numB[i] = Integer.parseInt(strnumB[i]);
}
//将题目给出的两个子集转化成Set集合
Set setA = new HashSet<>();
Set setB = new HashSet<>();
for (int i : numA) {
setA.add(i);
}
for (int i : numB) {
setB.add(i);
}
System.out.println("————答案如下:————");
//2.并集
Set Union = new HashSet<>();
Union.addAll(setA);
Union.addAll(setB);
System.out.println(Union);
//3.交集
Set intersection = new HashSet<>();
for (Integer eleA : setA) {
if (setB.contains(eleA)){//如果集合A和B共有这个元素,则为交集元素
intersection.add(eleA);
}
}
System.out.println(intersection);
//4.相对补集A-B
Set A_B = new HashSet<>();
for (Integer eleA : setA) {
if (!setB.contains(eleA)){//如果B不包含A的元素,则该元素是A独有
A_B.add(eleA);
}
}
System.out.println(A_B);
//5.相对补集B-A
Set B_A = new HashSet<>();
for (Integer eleB : setB) {
if (!setA.contains(eleB)){//如果B不包含A的元素,则该元素是A独有
B_A.add(eleB);
}
}
System.out.println(B_A);
//6.绝对补集A和B
Set complementaryA = new HashSet<>();
Set complementaryB = new HashSet<>();
for (Integer eleC : total) {
if (!setA.contains(eleC)){
complementaryA.add(eleC);
}
if (!setB.contains(eleC)){
complementaryB.add(eleC);
}
}
System.out.println(complementaryA);
System.out.println(complementaryB);
}
}
集合处理的过程和逻辑非常简单,但是生成网站规范的答案需要对结果进一步处理。本文主要关注集合处理过程,格式处理并未深入撰写。有大佬处理的话欢迎交流哈~



