PAT甲级——A1063 Set Similarity

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 Mintegers in the range [0]. After the input of sets, a positive integer K (≤) is given, followed by Klines 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%

Nc为两组数组中的公共数字的个数
Nt为去重后,两组数据的总个数

 1 #include <iostream>
 2 #include <unordered_set>//用set来去重
 3 using namespace std;
 4 int N, M, K, d;
 5 unordered_set<int>nums[55];
 6 int main()
 7 {
 8     cin >> N;
 9     for (int i = 1; i <= N; ++i)
10     {
11         cin >> M;
12         for (int j = 0; j < M; ++j)
13         {
14             cin >> d;
15             nums[i].insert(d);
16         }
17     }
18     cin >> K;
19     for (int i = 0; i < K; ++i)
20     {
21         int a, b;
22         double res, Nc = 0, Nt = 0;
23         cin >> a >> b;
24         for (auto v : nums[a])
25             if (nums[b].find(v) != nums[b].end())
26                 Nc++;
27         Nt = nums[a].size() + nums[b].size() - Nc;
28         res = (Nc / Nt)*100.0;
29         printf("%.1f%%\n", res);
30     }
31     return 0;
32 }

猜你喜欢

转载自www.cnblogs.com/zzw1024/p/11294909.html