Pat(Advanced Level)Practice--1063(Set Similarity)
来源:互联网 发布:淘宝怎么看申请时间 编辑:程序博客网 时间:2024/06/06 09:34
Pat1063代码
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:33 99 87 1014 87 101 5 877 99 101 18 5 135 18 9921 21 3Sample Output:
50.0%33.3%
#include<cstdio>#include<map>#include<set>#define N 55using namespace std;set<int> s[N];int main(int argc,char *argv[]){int n;int i,j;scanf("%d",&n);for(i=1;i<=n;i++){int m;int temp;scanf("%d",&m);for(j=1;j<=m;j++){scanf("%d",&temp);s[i].insert(temp);}}int k;scanf("%d",&k);while(k--){map<int,int> m;set<int>::iterator p;int x,y;scanf("%d%d",&x,&y);for(p=s[x].begin();p!=s[x].end();p++){int index=*p;m[index]++;}for(p=s[y].begin();p!=s[y].end();p++){int index=*p;m[index]++;}int count=0;map<int,int>::iterator it;for(it=m.begin();it!=m.end();it++)if(it->second>1)count++;printf("%.1f%%\n",100*count*1.0/m.size());}return 0;}
AC代码:
#include<cstdio>#include<set>#include<iterator>#include<algorithm>#define N 55using namespace std;set<int> s[N];int main(int argc,char *argv[]){int n;int i,j;scanf("%d",&n);for(i=1;i<=n;i++){int m;int temp;scanf("%d",&m);for(j=1;j<=m;j++){scanf("%d",&temp);s[i].insert(temp);}}int k;scanf("%d",&k);while(k--){int x,y;int ret,total;set<int> ans;scanf("%d%d",&x,&y);set_intersection(s[x].begin(),s[x].end(),s[y].begin(),s[y].end(),inserter(ans,ans.begin()));//求集合交集ret=ans.size();//交集元素个数total=s[x].size()+s[y].size()-ret;//不重复元素总个数printf("%.1f%%\n",100*ret*1.0/total);}return 0;}最后一个case跑了大概130ms;
#include<cstdio>#include<set>#include<iterator>#include<algorithm>#define N 55using namespace std;set<int> s[N];int GetUnion(int x,int y)//求并集元素的个数,其实algorithm里面有set_union{ //这个函数,在这里我们可以简单的实现一下int ret=0;set<int>::iterator it;for(it=s[x].begin();it!=s[x].end();it++){if(s[y].find(*it)==s[y].end())ret++;}ret+=s[y].size();return ret;}int main(int argc,char *argv[]){int n;int i,j;scanf("%d",&n);for(i=1;i<=n;i++){int m;int temp;scanf("%d",&m);for(j=1;j<=m;j++){scanf("%d",&temp);s[i].insert(temp);}}int k;scanf("%d",&k);while(k--){int x,y;int ret,total;scanf("%d%d",&x,&y);total=GetUnion(x,y);ret=s[x].size()+s[y].size()-total;printf("%.1f%%\n",100*ret*1.0/total);}return 0;}最后一个case跑了大概170ms;
- Pat(Advanced Level)Practice--1063(Set Similarity)
- PAT (Advanced Level) Practise 1063 Set Similarity (25)
- PAT (Advanced Level) Practise 1063 Set Similarity (25)
- 【PAT】【Advanced Level】1063. Set Similarity (25)
- 【C++ 】PAT(advanced level)1063. Set Similarity (25)
- PAT (Advanced Level) Practice 1006
- PAT (Advanced Level) 1063. Set Similarity (25) set的相似度
- 1063. Set Similarity (25)【集合操作】——PAT (Advanced Level) Practise
- PAT (Advanced) 1063. Set Similarity (25)
- Pat(Advanced Level)Practice--1025(PAT Ranking)
- Pat(Advanced Level)Practice--1075(PAT Judge)
- Pat(Advanced Level)Practice--1003(Emergency)
- Pat(Advanced Level)Practice--1004(Counting Leaves)
- Pat(Advanced Level)Practice--1008(Elevator)
- Pat(Advanced Level)Practice--1010(Radix)
- Pat(Advanced Level)Practice--1015(Reversible Primes)
- Pat(Advanced Level)Practice--1024(Palindromic Number)
- Pat(Advanced Level)Practice--1028(List Sorting)
- 【loadrunner】获取所有关联值中最后一个值的方法汇总
- H题 Max sum
- 微软云PC管理工具Windows Intune新版开测
- ios中用三种方式显示gif动态图片
- Linux下Tomcat的安装配置
- Pat(Advanced Level)Practice--1063(Set Similarity)
- C#常用知识积累
- 泛型
- UNDO相关问题总结(二)
- web.xml 中的listener、 filter、servlet 加载顺序及其详解
- 自主研发异步通信框架Minma(支持长连接和短连接)
- JAVA基础—关于Date
- linux下cmake编译C++工程之总结篇
- 用css3和javascript做的径向动画菜单