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 }

转载于:https://www.cnblogs.com/mxj961116/p/10348228.html

PAT A1063——set的常见用法详解相关推荐

  1. 《算法笔记》学习日记——6.1 vector的常见用法详解

    目录 6.1 vector的常见用法详解 问题 A: Course List for Student (25) 问题 B: Student List for Course (25) 小结 6.1 ve ...

  2. Linux中head和tail命令作用,Linux 命令head和tail常见用法详解

    head和tail是一组想对应的命令,默认分别显示文件的开头和末尾10行记录. head head 命令可以将一段文本的开头一部分输出到标准输出. head命令既可以处理文本文件也可以处理标准输入. ...

  3. linux lvm2,LVM2常见用法详解

    LVM2常见用法详解 1.简介 LVM(Logical Volume Manager)是逻辑卷管理的意思,是linux环境下对磁盘分区进行管理的一种机制,lvm是建立在硬盘和分区之上的一个逻辑层,来提 ...

  4. C++/C--unordered_map常见用法详解

    文章目录 1. std::unordered_map 的定义与特性 2. 构造 std::unordered_map 3. 赋值操作 4. 迭代器操作 4.1 指向整个容器中的元素 4.2 指向某个桶 ...

  5. C++/C--set常见用法详解【转载】

    1 概念 set是一个内部自动有序且不含重复元素的容器,其实现自动去重按升序排序.使用set,需要添加头文件:#include <set>.可以通过迭代器*it来访问set里面的元素,但是 ...

  6. C++中的unordered_map常见用法详解

    文章目录 1. std::unordered_map 的定义与特性 2. 构造 std::unordered_map 3. 赋值操作 4. 迭代器操作 4.1 指向整个容器中的元素 4.2 指向某个桶 ...

  7. python propresql mysql_python数据库操作mysql:pymysql、sqlalchemy常见用法详解

    本文实例讲述了python数据库操作mysql:pymysql.sqlalchemy常见用法.分享给大家供大家参考,具体如下: 相关内容: 使用pymysql直接操作mysql 创建表 查看表 修改表 ...

  8. dig命令的常见用法详解

    CATALOG 什么是dig 一个查询的例子 常见用法 什么是dig dig(域信息搜索器)命令是一个用于询问 DNS 域名服务器的灵活的工具.它执行 DNS 搜索,显示从受请求的域名服务器返回的答复 ...

  9. pair的常见用法详解

    目录 前言 pair的定义 pair中元素的访问 pair常用函数实例解析 pair的常见用途 前言 pair是一个很实用的"小玩意",当想要将两个元素绑在一起作为一个合成元素,又 ...

最新文章

  1. C++ 向量(vector) 的使用
  2. 2021年春季学期-信号与系统-第三次作业参考答案-第二道题
  3. python合并多个excel为一个_Python合并多个Excel数据
  4. 字节跳动最新开源!java界面实现查询功能
  5. python模块matplotlib.pyplot用法_Python中Matplotlib模块的简单使用
  6. java 清空stringbuffer_JAVA中清空StringBuffer变量
  7. 关于PyQt5,在pycharm上的安装步骤及使用技巧
  8. 创建Windows Mobile上兼容性好的UI 程序
  9. Perceptual Losses for Real-Time Style Transfer and Super-Resolution 运行程序
  10. 【三月专题】Dp专练
  11. ubuntu LVM
  12. 个人项目集 - Oliver Chu
  13. Tomcat 修改启动端口号
  14. win10开机提示服务未登录,无法加载用户配置文件
  15. Redmi Note 11Pro+(系统降级)
  16. 网络爬虫——爬取京东数据
  17. 明明现在科技发达了,互联网公司却纷纷搞起了996, 996没有未来
  18. c语言中字节数如何判断,C语言中怎样判断输入的是整数还是浮点数
  19. 2.Python-简单数据类型
  20. java 头尾 队列_java总结之 链表实现队列

热门文章

  1. 巧用Windows Phone应用商城中的应用链接
  2. 高可用keepalived实例
  3. 安装rational rose
  4. 如何快速安全的插入千万条数据?
  5. 学会这几个Redis技巧,让你的程序快如闪电
  6. 咖啡馆的故事:FTP, RMI , XML-RPC, SOAP, REST一网打尽
  7. 预见未来 | 数据智能的现在与未来
  8. Redis 高可用特性之 “持久化” 详解
  9. Spring Cloud构建微服务架构:服务消费(Feign)【Dalston版】
  10. Android --- Retrofit 之 Okhttp3 网络请求总是调用 onFailure 方法,而不调用 onResponse,报错 timeout。