一、技术总结

  1. 这个题目是属于set容器的内容,使用可以减少很多代码量
  2. 开始试过在同一个for循环中定义两个auto,结果编译通不过,有时候构思很重要,就比如这一题,开始我是一个一个去加,而代码中是,先同意其中一个容器中的所有数量,然后再遍历另一个容器,查找是否在该容器中有相同值,如果没有再在之前的所有数量上加一,这样就统计出来了两个set容器中值的并集。
  3. 同时,如何查找两个容器中的相同元素也是这个需要考查的点,这里使用find的函数,find函数是返回找到值的迭代器,如果没有找到就返回该容器的end()处迭代器,通过遍历其中一个set容器,然后调用find函数代码如下:
int Nt = q[compare[i][0]].size(), Nc = 0;//Nc表示共同元素的个数,Nt表示总共元素,即并集。
for(auto it = q[compare[i][1]].begin(); it != q[compare[i][1]].end(); it++){
if(q[compare[i][0]].find(*it) != q[compare[i][0]].end()) Nc++;
else Nt++;
}

二、参考代码

#include<iostream>
#include<set>
using namespace std;
const int N = 51;
const int M = 10000;
const int K = 2000;
set<int> q[N];
int compare[K][2];
double rate[K];
int main(){
int n, m, k;
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%d", &m);
int num;
for(int j = 0; j < m; j++){
scanf("%d", &num);
q[i].insert(num);
}
}
scanf("%d", &k);
for(int i = 0; i < k; i++){
scanf("%d %d", &compare[i][0], &compare[i][1]);
int Nt = q[compare[i][0]].size(), Nc = 0;//Nc表示共同元素的个数,Nt表示总共元素,即并集。
for(auto it = q[compare[i][1]].begin(); it != q[compare[i][1]].end(); it++){
if(q[compare[i][0]].find(*it) != q[compare[i][0]].end()) Nc++;
else Nt++;
}
rate[i] = Nc*100.0/Nt;
}
for(int i = 0; i < k; i++){
printf("%.1f%%\n", rate[i]);
}
return 0;
}

A1063 Set Similarity (25 分)的更多相关文章

  1. PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)

    1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be ...

  2. 1063 Set Similarity (25分)

    Given two sets of integers, the similarity of the sets is defined to be /, where N​c​​ is the number ...

  3. 【PAT甲级】1063 Set Similarity (25 分)

    题意: 输入一个正整数N表示集合的个数(<=50),接着输入N行,每行包括一个数字x代表集合的容量(<=10000),接着输入x个非负整数.输入一个正整数Q(<=2000),接着输入 ...

  4. 【算法笔记】A1063 Set Similarity

    1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be ...

  5. 1063. Set Similarity (25)

    1063. Set Similarity (25) 时间限制 300 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...

  6. PTA - - 06-图1 列出连通集 (25分)

    06-图1 列出连通集   (25分) 给定一个有NN个顶点和EE条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N-1N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发, ...

  7. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  8. PTA 字符串关键字的散列映射(25 分)

    7-17 字符串关键字的散列映射(25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位:再用除留余 ...

  9. PTA 旅游规划(25 分)

    7-10 旅游规划(25 分) 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条 ...

随机推荐

  1. Matplotlib绘图及动画总结

    目录 Matplotlib绘图总结 绘图原理 block模式(python默认) interactive模式(ipython模式默认) 深入子图 子图表示 子图绘图 绘制动画 参考链接 Matplot ...

  2. JdbcTemplate经典案例

    一.JdbcTemplate案例配置式 (1)导入依赖 <dependency> <groupId>org.springframework</groupId> &l ...

  3. 适合新手:从零开发一个IM服务端(基于Netty,有完整源码)

    本文由“yuanrw”分享,博客:juejin.im/user/5cefab8451882510eb758606,收录时内容有改动和修订. 0.引言 站长提示:本文适合IM新手阅读,但最好有一定的网络 ...

  4. operator ->重载是怎么做到的?

    https://stackoverflow.com/questions/8777845/overloading-member-access-operators-c struct client { in ...

  5. wpf datetime format

    <Style TargetType="{x:Type DatePickerTextBox}"> <Setter Property="Control.Te ...

  6. python处理sqlserver数据库的返回数据

    上代码: import SqlHelper.MSSQL as MS import pandas as pd if __name__ == '__main__': #连接数据库 ms = MS.MSSQ ...

  7. Linux常用命令(2)

    3.帮助命令 A,帮助命令:man    B,其他帮助命令 3.1,格式:man [命令名] 查看命令拥有哪个级别的帮助:                                       ...

  8. Implement Custom Business Classes and Reference Properties 实现自定义业务类和引用属性(XPO)

    In this lesson, you will learn how to implement business classes from scratch. For this purpose, the ...

  9. FCC---Animate Multiple Elements at Variable Rates---还可以改循环时间,达到不同律动频率的效果

    In the previous challenge, you changed the animation rates for two similarly animated elements by al ...

  10. CSS的border-radius 设置圆弧

    现象:将div变为有一定幅度的圆形.椭圆形等 方法:使用css的border-radius 属性进行设置CSS3 border-radius 属性:向 div 元素添加圆角边框: 一:首先建立一个di ...