set 常用函数实例

set是一个内部自动有序且不含重复元素的容器

(1)insert()

(2)find()  st.find(*it) 找到返回其迭代器,否者返回st.end()

(3)size()

(4)clear():清空所有元素

(5)erase():erase(st.begin()+3)删除第四个元素;erase(first,last)删除【first,last)内所有元素;

(6)如果set内定义的是结构体,需要重载<运算符
typedef struct Car {
int id;
int numForward;
int maxLayer;
double packetLoss;
}Car;
bool operator<(const Car &x,const Car &y) {
return x.id < y.id;
}
1063 Set Similarity

Given two sets of integers, the similarity of the sets is defined to be N​c​​/N​t​​×100%, where N​c​​ is the number of distinct common numbers shared by the two sets, and N​t​​ is the total number of distinct numbers in the two sets. Your job is to calculate the similarity of any given pair of sets.

Input Specification:

Each input file contains one test case. Each case first gives a positive integer N (≤50) which is the total number of sets. Then N lines follow, each gives a set with a positive M (≤10​4​​) and followed by M integers in the range [0,10​9​​]. After the input of sets, a positive integer K (≤2000) is given, followed by K lines of queries. Each query gives a pair of set numbers (the sets are numbered from 1 to N). All the numbers in a line are separated by a space.

Output Specification:

For each query, print in one line the similarity of the sets, in the percentage form accurate up to 1 decimal place.

Sample Input:

3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3

Sample Output:

50.0%
33.3%

题意:
  给出N个集合,给出的集合中可能含有相同的值。然后要求M个查询,每个查询给出两个集合的编号X和Y,求集合X和集合Y的相同元素率,即两个集合的交集和并集(均需去重)的元素个数的比率
参考代码:
 1 #include<cstdio>
2 #include<set>
3 using namespace std;
4 const int N = 51;
5 set<int> st[N]; //N个集合
6 void compare(int x, int y) { //比较集合st[x]和集合st[y]
7 int totalNum = st[y].size(), sameNum = 0; //不同数的个数,相同数个个数
8 //遍历集合st[x]
9 for(set<int>::iterator it = st[x].begin(); it != st[x].end(); it++) {
10 if(st[y].find(*it) != st[y].end()) sameNum++; //在st[y]中找到相同该元素
11 else totalNum++; //在st[y]中找不到相同元素
12 }
13 printf("%.1f%%\n", 100 * (double)sameNum/totalNum); //输出比率
14 }
15
16 int main(){
17 int n,k,q,v,st1,st2;
18 scanf("%d", &n); //集合个数
19 for(int i = 1; i <= n; i++) {
20 scanf("%d", &k); //集合i中的元素个数
21 for(int j = 0; j < k; j++) {
22 scanf("%d", &v); //集合i中的元素v
23 st[i].insert(v); //将元素v加入集合st[i]中
24 }
25 }
26 scanf("%d", &q); //q个查询
27 for(int i = 0; i < q; i++) {
28 scanf("%d%d", &st1, &st2); //欲对比的集合编号
29 compare(st1, st2); //比较两个集合
30 }
31 return 0;
32 }


PAT A1063——set的常见用法详解的更多相关文章

  1. PAT A1060——string的常见用法详解

    string 常用函数实例 (1)operator += 可以将两个string直接拼接起来 (2)compare operator 可以直接使用==.!=.<.<=.>.>= ...

  2. STL pair 常见用法详解

    <算法笔记>学习笔记 pair 常见用法详解 //pair是一个很实用的"小玩意",当想要将两个元素绑在一起作为一个合成元素, //又不想因此定义结构体时,使用pair ...

  3. STL stack 常见用法详解

    <算法笔记>学习笔记 stack 常见用法详解 stack翻译为栈,是STL中实现的一个后进先出的容器.' 1.stack的定义 //要使用stack,应先添加头文件#include &l ...

  4. STL priority_queue 常见用法详解

    <算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...

  5. STL queue 常见用法详解

    <算法笔记>学习笔记 queue 常见用法详解 queue翻译为队列,在STL中主要则是实现了一个先进先出的容器. 1. queue 的定义 //要使用queue,应先添加头文件#incl ...

  6. STL map 常见用法详解

    <算法笔记>学习笔记 map 常见用法详解 map翻译为映射,也是常用的STL容器 map可以将任何基本类型(包括STL容器)映射到任何基本类型(包括STL容器) 1. map 的定义 / ...

  7. STL string 常见用法详解

    string 常见用法详解 1. string 的定义 //定义string的方式跟基本数据类型相同,只需要在string后跟上变量名即可 string str; //如果要初始化,可以直接给stri ...

  8. STL set 常见用法详解

    <算法笔记>学习笔记 set 常见用法详解 set是一个内部自动有序且不含重复元素的容器 1. set 的定义 //单独定义一个set set<typename> name: ...

  9. STL vector常见用法详解

    <算法笔记>中摘取 vector常见用法详解 1. vector的定义 vector<typename> name; //typename可以是任何基本类型,例如int, do ...

随机推荐

  1. Bert文本分类实践(一):实现一个简单的分类模型

    写在前面 文本分类是nlp中一个非常重要的任务,也是非常适合入坑nlp的第一个完整项目.虽然文本分类看似简单,但里面的门道好多好多,作者水平有限,只能将平时用到的方法和trick在此做个记录和分享,希 ...

  2. Linux7安装redis6

    首先下载软件包并解压 cd /opt wget https://download.redis.io/releases/redis-6.2.5.tar.gz tar -zxvf redis-6.2.5. ...

  3. Java基础之(十一):方法

    Java方法详解 何谓方法(了解) System.out.println() // 类 对象 方法 方法是语句的集合,它们在一起执行一个功能. 方法是解决一类问题的步骤的有序集合 方法包含于类或对象中 ...

  4. 用C++实现的数独解题程序 SudokuSolver 2.2 及实例分析

    SudokuSolver 2.2 程序实现 根据 用C++实现的数独解题程序 SudokuSolver 2.1 及实例分析 里分析,对 2.1 版做了一些改进和尝试. CQuizDealer 类声明部 ...

  5. SpringMVC 获得请求数据

    获得请求参数 客户端请求参数的格式是:name=value&name=value- - 服务器端要获得请求的参数,有时还需要进行数据的封装,SpringMVC可以接收如下类型的参数: 基本类型 ...

  6. VS Code C/C++开发环境配置

    VS Code C/C++开发环境配置 一.安装 ​ 1.前往官网下载安装即可 https://code.visualstudio.com/ ​ 2.进入VS Code安装如下插件 二.C/C++开发 ...

  7. '\r'(回车符),'\n'(换行符)与"\r\n"

    一.'\n','\r'和"\r\n" 回车\r本义是光标重新回到本行开头,r的英文return,控制字符可以写成CR,即Carriage Return(回车,carriage有&q ...

  8. 莱特币(LTC)开发环境搭建

    Linux系统下搭建莱特币LTC开发环境 1.简介 2.LTC 客户端下载 3.解压到一个固定的目录中 4.启动客户端 4.1.参数解释: 4.2.更多详细的配置 5.执行命令测试一下 6.获取莱特币 ...

  9. 常用Java API:Math类

    求最值 最小值 Math.min(int a, int b) Math.min(float a, float b) Math.min(double a, doubleb) Math.min(long ...

  10. IE浏览器——网络集合代理无法启动

    用管理员身份运行cmd然后输入 sc config diagnosticshub.standardcollector.service start=demand