c++ set 交集 並集 差集
《Problem A: 求集合的交併補集》
Time Limit: 1 Sec Memory Limit: 4 MBSubmit: 973 Solved: 242
[Submit][Status][Web Board]
Description
任意給定兩個包含1-30000個元素的集合A,B(集合中元素型別為任意整型數,且嚴格遞增排列),求A交B、A並B、A-B和B-A集合。
Input
輸入第一行為測試資料組數。每組測試資料兩行,分別為集合A、B。每行第一個數n(1<=n<=30000)為元素數量,後面有n個嚴格遞增的絕對值小於2^31代表集合中包含的數。
Output
對每組測試資料輸出5行,第1行為資料組數,後4行分別為按升序輸出兩個集合的A交B、A並B、A-B和B-A集合。格式見樣例。
Sample Input
1
3 1 2 5
4 2 3 5 8
Sample Output
Case #1:
2 5
1 2 3 5 8
1
3 8
法1:
<span style="font-size:18px;">#include <iostream> #include <set> #include <algorithm> using namespace std; void put_in(set<int> &p){ int q; cin>>q; while(q --){ int value; cin>>value; p.insert(value); } } void put_out(set<int> &p){ set<int>::iterator it = p.begin(); while(it != p.end()){ cout<<*it<<" "; it ++; } } int main(){ int T,T1=1; cin>>T,T1; while(T1 <= T){ set<int> A,B,RUnion,RIntersec,RDiff1,RDiff2; put_in(A); put_in(B); set_intersection(A.begin(),A.end(),B.begin(),B.end(),inserter(RIntersec,RIntersec.begin())); set_union(A.begin(),A.end(),B.begin(),B.end(),inserter(RUnion,RUnion.begin())); set_difference(A.begin(),A.end(),B.begin(),B.end(),inserter(RDiff1,RDiff1.begin())); set_difference(B.begin(),B.end(),A.begin(),A.end(),inserter(RDiff2,RDiff2.begin())); cout<<"Case #"<<T1 ++<<":"<<endl; put_out(RIntersec); cout<<endl; put_out(RUnion); cout<<endl; put_out(RDiff1); cout<<endl; put_out(RDiff2);cout<<endl; } return 0; }</span>
法2:
#include <iostream> #include <set> #include <algorithm> using namespace std; void put_in(set<int> &p){ int q; cin>>q; while(q --){ int value; cin>>value; p.insert(value); } } void put_out(set<int> &p){ set<int>::iterator it = p.begin(); while(it != p.end()){ cout<<*it<<" "; it ++; } } void Intersection(set<int> &A,set<int> &B,set<int> &result){ set<int>::iterator it; it = A.begin(); while(it != A.end()){ if(B.find(*it) != B.end()) result.insert(*it); it++; } } void Union(set<int> &A,set<int> &B,set<int> &result){ set<int>::iterator it; it = A.begin(); while(it != A.end()){ result.insert(*it); it++; } it = B.begin(); while(it != B.end()){ result.insert(*it); it++; } } void Difference(set<int> &A,set<int> &B,set<int> &result){ set<int>::iterator it; it = A.begin(); while(it != A.end()){ if(B.find(*it) == B.end()) result.insert(*it); it++; } } int main(){ int T,T1=1; cin>>T,T1; while(T1 <= T){ set<int> A,B,RUnion,RIntersec,RDiff1,RDiff2; put_in(A); put_in(B); Intersection(A,B,RIntersec); Union(A,B,RUnion); Difference(A,B,RDiff1); Difference(B,A,RDiff2); cout<<"Case #"<<T1 ++<<":"<<endl; put_out(RIntersec); cout<<endl; put_out(RUnion); cout<<endl; put_out(RDiff1); cout<<endl; put_out(RDiff2); cout<<endl; } return 0; }
相關推薦
c++ set 交集 並集 差集
《Problem A: 求集合的交併補集》 Time Limit: 1 Sec Memory Limit: 4 MB Submit: 973 Solved: 242 [Submit][Status][
C# 取兩個集合的交集並集差集
兩個 color pre str exce class 並集 blog span 交集:Intersect 並集:Union 差集:Except var A= new List() { 1, 2, 3, 4, 5, 6 }; var B= new List() { 3
遞增有序的順序表表示集合,求解兩個集合的交集 並集 差集(c語言實現)
#include<stdio.h> #include<stdlib.h> #define max 100 typedef struct { int elem[max]; int length; }List; void UnionLi
STL 演算法vector/set集合-交集,並集,差集,對稱差
針對這裡提及的四個集合運算必須特別注意: 1、第一個演算法需保證第一集合和第二集合有序,並從小到大排序,內部使用預設“<”操作符比較元素大小; 2、第二個演算法需保證第一集合和第二集合有序,排序方式參照Compare確定,內部使用Compare比較元素大小。 1 --
Python 求兩個文本文件以行為單位的交集 並集 差集
cti %s txt readlines nio 兩個 open inter class Python 求兩個文本文件以行為單位的交集 並集 差集,來代碼: s1 = set(open(‘a.txt‘,‘r‘).readlines()) s2 = set(
java list 交集 並集 差集 去重複並集
package com; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Test { public static void ma
js求物件陣列的交集/並集/差集/去重
1.求交集 var arr1 = [{name:'name1',id:1},{name:'name2',id:2},{name:'name3',id:3}]; var arr1Id = [1,2,3] var arr2 = [{name:'name1',id
容器的交集並集差集
使用泛型演算法,mark一下 #include <vector> #include <iostream> #include <iterator> #include <algorithm> int main() { std::vector<
求陣列的交集-並集-差集
let a = new Set([1, 2, 3]); let b = new Set([4, 3, 2]); // 並集 let union = new Set([...a, ...b]); //Set(4) {1, 2, 3, 4} // 交集 let in
採用java8 lambda表示式 實現 java list 交集 並集 差集 去重複並集
採用java8 lambda表示式 實現java list 交集/並集/差集/去重並集 一般的javaList 交、並集採用簡單的 removeAll retainAll 等操作,不過這也破壞了原始的javaList物件,採用java8 lambda表示式流操
java 兩個list 交集 並集 差集 去重複並集
List<String> list1 =new ArrayList<String>();list1.add("A");list1.add("B);List<String&
JAVA工具類學習-java 兩個list 交集 並集 差集 去重複並集
List<String> list1 =new ArrayList<String>();list1.add("A");list1.add("B);List<String> list2 =new ArrayList<String>
資料庫並集union、交集intersect、差集except
資料庫對兩個或多個結果集進行合併、取重、剔除操作時,可以通過UNION、INTERSECT、EXCEPT來實現。 所操作的結果集有如下限制條件: (1)所有查詢中的列數和列的順序必須相同。 (2)比較
SQLSERVER資料集合的交、並、差集運算(intersect,union,except)
SQLServer中通過intersect,union,except和三個關鍵字對應交、並、差三種集合運算。 他們的對應關係可以參考下面圖示 測試示例: 構造A,B兩個資料集 A:1,2
Python set運算 集合差集,並集,交集,list去重復
bsp 一行 color nbsp 方便 移除 line pytho 差集 在沒有發現方便的set運算之前,都是用遍歷list查找兩個集合的差別。 比如, 找list1和list2的差集 for i in list1: if not i in list2:
【轉】C# Linq 交集、並集、差集、去重
log .cn pre tin nio clas int except post 轉自: https://www.cnblogs.com/wdw31210/p/4167306.html using System.Linq; List<string&
Java中使用Set進行並集,差集,交集查詢
利用Java中使用Set進行並集,差集,交集查詢 首先命名一個類名為DealSet存放查詢並集,差集,交集的方法 DealSet.java package SetLearning; import java.util.HashSet; import java.util.Set; p
Java之兩個Set集合的交集、差集和並集
一、求交集 注:場景是讀取兩個檔案,把檔案內容放到Set中,求兩個檔案之間的共同元素。在這裡只寫對Set的操作。 public static void main(String[] args) throws Exception { String path1 = "pat
C# Linq 交集、並集、差集、去重
using System.Linq; List<string> ListA = new List<string>(); List<string> ListB = new List<string>(); List<string> ListRe
求List,Map,Set的交集,並集與差集
應用場景 在大資料的背景下,我們在做專案的時候往往使用單表在資料庫中查詢資料,然後多表在service層進行關聯操作。比如說下面的情況就是如此,在這裡我並不是展開講多表之間如何實現解耦的單表查詢操作,我只是針對其中的涉及多表關聯的集合操作進行講解