Given two sets of integers, the similarity of the sets is defined to be /, 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 (≤) which is the total number of sets. Then N lines follow, each gives a set with a positive M (≤) and followed by M integers in the range [0]. After the input of sets, a positive integer K (≤) 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%
题目分析:利用map对应存储需要的键与值 然后从键少的那个遍历 看另一个map中有没有对应的值 注
不能通过直接访问来判断某个键值是否存在(在map<int int>的情况下 访问不存在的键 会生成相应的键值对 其值默认为0)要通过map中find函数来判断键值是否存在
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <climits> 3 #include<iostream> 4 #include<vector> 5 #include<queue> 6 #include<map> 7 #include<set> 8 #include<stack> 9 #include<algorithm> 10 #include<string> 11 #include<cmath> 12 using namespace std; 13 map<int, int> M[50]; 14 int Size[50]; 15 int main() 16 { 17 int N; 18 cin >> N; 19 for (int i = 0; i < N; i++) 20 { 21 int K; 22 cin >> K; 23 for (int j = 0; j <K; j++) 24 { 25 int num; 26 cin >> num; 27 M[i][num]++; 28 Size[i]++; 29 } 30 } 31 int K; 32 cin >> K; 33 for (int i = 0; i < K; i++) 34 { 35 int v1, v2; 36 int trueHave = 0; //相等元素的个数 37 cin >> v1 >> v2; 38 v1--; 39 v2--; 40 int size = M[v1].size() + M[v2].size(); 41 int v = (Size[v1] < Size[v2]) ? v1 : v2; 42 int d = (Size[v1] < Size[v2]) ? v2 : v1; 43 for (auto it : M[v]) 44 if (M[d].find(it.first)!=M[d].end()) 45 trueHave++; 46 printf("%.1f%% ", (1.0 * trueHave) / (1.0 * (size-trueHave))*100.0); 47 } 48 }