PAT A1063——set的常见用法详解
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)内所有元素;
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;
}
Given two sets of integers, the similarity of the sets is defined to be Nc/Nt×100%, where Nc is the number of distinct common numbers shared by the two sets, and Nt 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 (≤104) and followed by M integers in the range [0,109]. 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的常见用法详解的更多相关文章
- PAT A1060——string的常见用法详解
string 常用函数实例 (1)operator += 可以将两个string直接拼接起来 (2)compare operator 可以直接使用==.!=.<.<=.>.>= ...
- STL pair 常见用法详解
<算法笔记>学习笔记 pair 常见用法详解 //pair是一个很实用的"小玩意",当想要将两个元素绑在一起作为一个合成元素, //又不想因此定义结构体时,使用pair ...
- STL stack 常见用法详解
<算法笔记>学习笔记 stack 常见用法详解 stack翻译为栈,是STL中实现的一个后进先出的容器.' 1.stack的定义 //要使用stack,应先添加头文件#include &l ...
- STL priority_queue 常见用法详解
<算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...
- STL queue 常见用法详解
<算法笔记>学习笔记 queue 常见用法详解 queue翻译为队列,在STL中主要则是实现了一个先进先出的容器. 1. queue 的定义 //要使用queue,应先添加头文件#incl ...
- STL map 常见用法详解
<算法笔记>学习笔记 map 常见用法详解 map翻译为映射,也是常用的STL容器 map可以将任何基本类型(包括STL容器)映射到任何基本类型(包括STL容器) 1. map 的定义 / ...
- STL string 常见用法详解
string 常见用法详解 1. string 的定义 //定义string的方式跟基本数据类型相同,只需要在string后跟上变量名即可 string str; //如果要初始化,可以直接给stri ...
- STL set 常见用法详解
<算法笔记>学习笔记 set 常见用法详解 set是一个内部自动有序且不含重复元素的容器 1. set 的定义 //单独定义一个set set<typename> name: ...
- STL vector常见用法详解
<算法笔记>中摘取 vector常见用法详解 1. vector的定义 vector<typename> name; //typename可以是任何基本类型,例如int, do ...
随机推荐
- JVM学习笔记——GC算法
GC 算法 GC 即 Garbage Collection 垃圾回收.JVM 中的 GC 99%发生在堆中,而 Java 堆中采用的垃圾回收机制为分代收集算法.即将堆分为新生代和老年代,根据不同的区域 ...
- react之组建通信
父组件与子组件通信 父组件将自己的状态传递给子组件,子组件当做属性来接收,当父组件更改自己状态的时候,子组件接收到的属性就会发生改变 父组件利用ref对子组件做标记,通过调用子组件的方法以更改子组件的 ...
- PAT (Basic Level) Practice (中文)1014 福尔摩斯的约会 (20分)
1014 福尔摩斯的约会 (20分) 带侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hys ...
- tcl概述
tcl,全名tool command language,是一种通用的工具语言. 1)每个命令之间,通过换行符或者分号隔开: 2)tcl的每个命令包含一个或者多个单词,默认第一个单词表示命令,第二个单词 ...
- javascript-jquery对象的属性处理
1.attr()方法:获取元素某个属性的值. $("img").attr("title");//获得第一个<img>元素的title属性 $(&qu ...
- Golang通脉之接口
接口(interface)定义了一个对象的行为规范,只定义规范不实现,由具体的对象来实现规范的细节. 接口类型 在Go语言中接口(interface)是一种类型,一种抽象的类型. interface是 ...
- 实用小工具:screen
实用小工具:screen 首先,吹爆screen screen,实现了不间断的会话服务,通过SSH连接至远程服务器,当使用了screen开启的会话,不会因为你断开SSH而中断在远程服务器上运行的命令. ...
- 第2次 Beta Scrum Meeting
本次会议为Beta阶段第2次Scrum Meeting会议 会议概要 会议时间:2021年5月31日 会议地点:「腾讯会议」线上进行 会议时长:0.5小时 会议内容简介:对完成工作进行阶段性汇报:对下 ...
- Noip模拟18 2021.7.17 (文化课专场)
T1 导弹袭击(数学) 显然,我们要找到最优的A,B使得一组a,b优于其他组那么可以列出: $\frac{A}{a_i}+\frac{B}{b_i}<\frac{A}{a_j}+\frac{B} ...
- 关于把RTL工程代码封装成IP时对define宏定义参数的处理
在把RTL工程封装成IP的时候,如果工程中的代码中含有global include中定义的参数,则vivado不支持该参数文件的封装.出现IP_FLOW 19-4646的错误代码,解决方法: 1.在用 ...