1063. Set Similarity (25)
2015-09-05 17:17
211 查看
Set Similarity (25)
时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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%
今天学习力set和multiset,对两者的了解更深一层。
这题刚开始没有理解正确,使用了multiset解题,花了近一个小时。。。
当然最后结果应该是正确的,但是输出竟然出问题了。
原因是因为我是先计算再转化为浮点型,这个过程中可能会丢数。只要先转化浮点型就行了。
附上使用mutilset解题的代码,也能输出正确结果,自我感觉思路还是不错的,虽然一开始就走入了岔道。。
时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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%
今天学习力set和multiset,对两者的了解更深一层。
这题刚开始没有理解正确,使用了multiset解题,花了近一个小时。。。
当然最后结果应该是正确的,但是输出竟然出问题了。
原因是因为我是先计算再转化为浮点型,这个过程中可能会丢数。只要先转化浮点型就行了。
#include<iostream> #include<set> #include<vector> using namespace std; int main() { //freopen("in.txt","r",stdin); int N,K; cin>>N; set <int> m[50]; for(int i=0;i<N;i++) { int M,c; cin>>M; for(int j=0;j<M;j++) { cin>>c; m[i].insert(c); } } cin>>K; int m1,m2; while(K--) { cin>>m1>>m2; set<int>::const_iterator sm2=m[m2-1].begin(); int total=m[m1-1].size()+m[m2-1].size(); int same=0;//相同数的总数 while(sm2!=m[m2-1].end()) { if(m[m1-1].find(*sm2)!=m[m1-1].end()) same++; sm2++; } printf("%.1f%%\n", same*1.0/(total-same)*100); //float bili=(100*same)/(total-same); //printf("%.1f%%\n",bili); } system("pause"); return 0; }
附上使用mutilset解题的代码,也能输出正确结果,自我感觉思路还是不错的,虽然一开始就走入了岔道。。
#include<iostream> #include<set> #include<vector> using namespace std; int main() { freopen("in.txt","r",stdin); int N,K; cin>>N; set <int> m[50]; for(int i=0;i<N;i++) { int M,c; cin>>M; for(int j=0;j<M;j++) { cin>>c; m[i].insert(c); } } cin>>K; int m1,m2; while(K--) { cin>>m1>>m2; set<int>::const_iterator sm1=m[--m1].begin(); set<int>::const_iterator sm2=m[--m2].begin(); int total=m[m1].size()+m[m2].size(); int same=0;//相同数的总数 int lastsame=-1; int samenum=0;////相同数的总类 while(sm1!=m[m1].end()&&sm2!=m[m2].end()) { if(*sm1==*sm2) { if(lastsame!=*sm1) { samenum++; lastsame=*sm1;//上一个相同的数 } sm1++; sm2++; same=same+2; continue; } else { if(*sm1==lastsame) { sm1++; same++; continue; } else if(*sm2==lastsame) { sm2++; same++; continue; } if(*sm1<*sm2) { int mm=*sm1; sm1++; if(mm==*sm1) total--; } else { int mm=*sm2; sm2++; if(mm==*sm2) total--; } } } while(sm2!=m[m2].end()) { if(*sm2==lastsame) { sm2++; same++; } else { int mm=*sm2; sm2++; if(sm2==m[m2].end()) break; if(*sm2==mm) total--; } } while(sm1!=m[m1].end()) { if(*sm1==lastsame) { sm1++; same++; } else { int mm=*sm1; sm1++; if(sm1==m[m1].end()) break; if(*sm1==mm) total--; } } float bili=100.0*samenum/(total-same+samenum); printf("%.1f",bili);cout<<"%"<<endl; } system("pause"); return 0; }
相关文章推荐
- libdvbpsi源码分析(四)PAT表解析/重建
- PAT配置
- 什么是端口复用动态地址转换(PAT) 介绍配置实例
- MikroTik layer7-protocol
- PAT是如何工作的
- PAT 乙级题:1002. 写出这个数 (20)
- PAT (Advanced Level) Practise 1001-1010
- 数据结构学习与实验指导(一)
- PAT Basic Level 1001-1010解题报告
- 1001. 害死人不偿命的(3n+1)猜想
- 1002. 写出这个数
- 1032. 挖掘机技术哪家强
- 1001. 害死人不偿命的(3n+1)猜想 (PAT basic)
- 1002. 写出这个数(PAT Basic)
- 1004. 成绩排名(PAT Basic)
- 1006. 换个格式输出整数(PAT Basic)
- 1007. 素数对猜想(PAT Basic)
- 1008. 数组元素循环右移问题
- 1009. 说反话(PAT Basic)
- 1011. A+B和C(PAT Basic)