1063 Set Similarity (25 分) java 题解
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中即可。
注意局部变量的使用和输出的格式。
java代码:
import java.io.*;
import java.util.*;public class Main {public static void main(String[] args) throws NumberFormatException, IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int n = Integer.parseInt(br.readLine());int [][]arr = new int[n][];for(int i = 0; i < n;i++) {String[] split = br.readLine().split(" ");int m = Integer.parseInt(split[0]);arr[i] = new int[m];for(int j = 0; j < m;j++) {arr[i][j] = Integer.parseInt(split[j + 1]);}}int count = Integer.parseInt(br.readLine());StringBuilder builder = new StringBuilder();for(int i = 0; i < count;i++) {int ans = 0;String[] split = br.readLine().split(" ");int a = Integer.parseInt(split[0]) - 1;int b = Integer.parseInt(split[1]) - 1;Set<Integer> set = new HashSet<>();Set<Integer> set1 = new HashSet<>();for(int j = 0; j < arr[a].length;j++) {set.add(arr[a][j]);}for(int j = 0; j < arr[b].length;j++) {boolean key = set.contains(arr[b][j]);boolean key1 = set1.contains(arr[b][j]);if(key && !key1) {ans++;}set1.add(arr[b][j]);}set.addAll(set1);String str = String.format("%.3f",(ans * 1.0) / set.size());builder.append(String.format("%.1f%%\n",Float.parseFloat(str) * 100));
// System.out.printf("%.1f%%\n",Float.parseFloat(str) * 100);}System.out.print(builder.toString().trim());}
}
PTA提交截图:
1063 Set Similarity (25 分) java 题解相关推荐
- 1080 MOOC期终成绩 (25 分) java 题解
题目描述: 对于在中国大学MOOC(http://www.icourse163.org/ )学习"数据结构"课程的学生,想要获得一张合格证书,必须首先获得不少于200分的在线编程作 ...
- 【最后测试点超时】1063 Set Similarity (25 分)_22行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given two sets of integers, the similarity of the sets is defined ...
- 1063 Set Similarity (25 分)【难度: 一般 / 知识点: STL中set的应用】
https://pintia.cn/problem-sets/994805342720868352/problems/994805409175420928 可以很容易的写出一个最暴力的写法.可以骗21 ...
- 1007 Maximum Subsequence Sum (25 分) java 题解
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { N ...
- 1074 Reversing Linked List (25 分) java 题解
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- L2-031 深入虎穴 (25 分) Java题解 (树的最大深度dfs,bfs)
输入样例: 13 3 2 3 4 2 5 6 1 7 1 8 1 9 0 2 11 10 1 13 0 0 1 12 0 0 输出样例: 12 解题思路: 找最大深度的编号,由于答案唯一,所以宽搜到的 ...
- 【PAT甲级 补全前导0 vector作为函数参数】1025 PAT Ranking (25 分) Java、C++
如何补全前导0 Java String idStr = String.format("%013d", out.id); //用0补全13位 System.out.println(i ...
- 【PAT甲级 BigInteger运算】1024 Palindromic Number (25 分) Java 全部AC
题目 题解 Java 一开始用C++写的,有两个大数的测试点过不去,后来改用Java的BigInteger开挂,全部通过 import java.io.BufferedReader; import j ...
- pat题解java,1039 到底买不买 (20分) Java题解 PAT (Basic Level) Practice (中文)- 巧妙开大数组减少代码量...
1039 到底买不买 (20分) 原题链接:传送门 一.题目: 输入样例 1: ppRYYGrrYBR2258 YrR8RrY 输出样例 1: Yes 8 输入样例 2: ppRYYGrrYB225 ...
最新文章
- 腾讯AI Lab的研究方向
- [2020.11.27NOIP模拟赛]中位数之中位数【二分,树状数组】
- 如何判断自己的编程水平
- Dojo API中文 Dojo内容模块概览,初学者
- pads中如何设置等长_标签打印软件中标签间距以及边距如何设置
- 综合能源管理服务认证是什么 , 综合能源服务认证有什么要求?
- python如何截长图_python 截长图、H5页面截长图
- java面向对象怎样建类和对象_java面向对象(上)-- 类与对象、类的封装
- php-screw 安装,liunx 下安装 php_screw 扩展 以及报错处理
- Ardupilot Pre-Arm安全检查程序分析
- 出场顺序很重要下一句_一首年少有为告诉你:人生的出场顺序真的很重要
- STM32学习笔记(一)
- 蓝桥杯单片机(八)DS18B20温度测量(整数显示)
- CSS——CSS基础知识
- 快捷键大全(实用版)
- PageHelper处理分页问题,total总数不对
- 海银资本:在大数据创业企业中掘金
- 一起智慧课堂_聚焦课堂,提升质量——六盘水市钟山区第一小学“智慧”课堂校际交流活动...
- C++ 宽、窄字符转换
- 关于编译WebRTC Linux/Android版源码的说明