PAT_甲级_1063

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lzc842650834/article/details/88139702

1063 Set Similarity

Given two sets of integers, the similarity of the sets is defined to be N​c​​ /N​t​​ ×100%, where N​c​​ is the number of distinct common numbers shared by the two sets, and N​t​​ 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 (≤10​4​​ ) and followed by M integers in the range [0,10​9​​ ]. 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%

注意0-N-1和1-N的互换

#include<set>
#include<iostream>
#include<stdio.h>
using namespace std;
set<int> arr[60];
int main()
{
    int num, temp, m;
    cin >> num;
    for (int i = 0; i < num; i++) {
        cin >> m;
        for (int j = 0; j < m; j++) {
            scanf("%d", &temp);
            arr[i].insert(temp);
        }
    }
    int num_q;
    cin >> num_q;
    for (int i = 0; i < num_q; i++) {
        int temp_1, temp_2;
        cin >> temp_1 >> temp_2;
        temp_1--;
        temp_2--;
        int re_1 = arr[temp_1].size() + arr[temp_2].size();
        int re_2 = 0;
        for (set<int>::iterator it = arr[temp_2].begin(); it != arr[temp_2].end(); it++) {
            if (arr[temp_1].find(*it) != arr[temp_1].end()) {
                re_1--;
                re_2++;
            }
        }
        double re = re_2 * 100.0 / re_1 ;
        printf("%.1f%%\n", re);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lzc842650834/article/details/88139702