时间限制
400 ms
内存限制
64 MB
题目描述:
给定两个整数集合,它们的相似度定义为: N c / N t ∗ 100 % N_{c} / N_{t} * 100\% Nc/Nt∗100%。其中N c 是两个集合都有的不相等整数的个数, N t N_{t} Nt 是两个集合一共有的不相等整数的个数。你的任务就是计算任意一对给定集合的相似度。
输入格式:
输入第一行给出一个正整数N(≤50),是集合的个数。随后N行,每行对应一个集合。每个集合首先给出一个正整数M(≤
1
0
4
10^{4}
104 ),是集合中元素的个数;然后跟M个[0,
1
0
9
10^{9}
109 ]区间内的整数。
之后一行给出一个正整数K(≤2000),随后K行,每行对应一对需要计算相似度的集合的编号(集合从1到N编号)。数字间以空格分隔。
输出格式:
对每一对需要计算的集合,在一行中输出它们的相似度,为保留小数点后2位的百分比数字。
输入样例:
3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3
输出样例:
50.00%
33.33%
给定n个集合
再给定k组 要进行计算相似度的两个集合
求出
这两个集合不重复的相同的个数
这两个集合中不重所有个数
\frac{这两个集合不重复的相同的个数}{这两个集合中不重所有个数}
这两个集合中不重所有个数这两个集合不重复的相同的个数
emmmmmmm
set将每个集合弄成不重复的
然后 一个一个去填充到res中
两个集合相同的个数 = 两个集合的个数 - 两个集合合并之后不同的个数
答案就是
两个集合相同的数
两个集合合并之后不同的个数
\frac{两个集合相同的数}{两个集合合并之后不同的个数}
两个集合合并之后不同的个数两个集合相同的数
注: java 会T 数据量有亿点大
暴力
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n = sc.nextInt();
TreeSet<Integer> shu[] = new TreeSet[n + 10];
for (int i = 1; i <= n; i++)
{
int cnt = sc.nextInt();
shu[i] = new TreeSet<Integer>();
while (cnt-- > 0)
{
int x = sc.nextInt();
shu[i].add(x);
}
}
int k = sc.nextInt();
TreeSet<Integer> res = new TreeSet<Integer>();
while (k-- > 0)
{
int a = sc.nextInt();
int b = sc.nextInt();
res.clear();
for (int i : shu[a])
res.add(i);
for (int i : shu[b])
res.add(i);
out.printf("%.2f%%\n", (shu[a].size() + shu[b].size() - res.size()) * 100.0 / res.size());
}
out.flush();
out.close();
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}
Set 的 addAll函数
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n = sc.nextInt();
TreeSet<Integer> shu[] = new TreeSet[n + 10];
for (int i = 1; i <= n; i++)
{
int cnt = sc.nextInt();
shu[i] = new TreeSet<Integer>();
while (cnt-- > 0)
{
int x = sc.nextInt();
shu[i].add(x);
}
}
int k = sc.nextInt();
TreeSet<Integer> res = new TreeSet<Integer>();
while (k-- > 0)
{
int a = sc.nextInt();
int b = sc.nextInt();
res.clear();
res.addAll(shu[a]);
res.addAll(shu[b]);
out.printf("%.2f%%\n", (shu[a].size() + shu[b].size() - res.size()) * 100.0 / res.size());
}
out.flush();
out.close();
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}
c++
#include <cstdio>
#include <unordered_set>
using namespace std;
const int N = 50;
unordered_set<int> shu[N + 10];
unordered_set<int> res;
int main()
{
int n; scanf("%d", &n);
for(int i = 1; i <= n; i ++)
{
int cnt; scanf("%d", &cnt);
while(cnt -- > 0)
{
int x; scanf("%d", &x);
shu[i].insert(x);
}
}
int k; scanf("%d", &k);
while(k -- > 0)
{
int a, b; scanf("%d%d", &a, &b);
res.clear();
for(int i : shu[a]) res.insert(i);
for(int i : shu[b]) res.insert(i);
printf("%.2f%%\n", (shu[a].size() + shu[b].size() - res.size()) * 100.0 / res.size());
}
return 0;
}
如果有说错的 或者 不懂的 尽管提 嘻嘻
一起进步!!!