Be cautious when using Map and Set in Java
2015-07-31 14:00
507 查看
HashMap and HashSet in Java
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84400Problem D from Ecust match
import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; public class Main { public int MAX_K = 1002; public int n; public int[] a = new int[2005]; public int[] b = new int[2005]; // The position of each point private HashMap<String, HashSet<String>> datas; public static void main(String[] s) { Main context = new Main(); context.fMain(); } private void fMain() { Scanner sc = new Scanner(System.in); n = sc.nextInt(); a[0] = sc.nextInt(); b[0] = sc.nextInt(); datas = new HashMap<String, HashSet<String>>(); // HashSet<Point> points = new HashSet<Point>(); // HashSet will never allow its child to be the same. // Map will ensure all the keys will be the unique. for (int i = 1; i < n; i++) { a[i] = sc.nextInt(); b[i] = sc.nextInt(); fAdd(a[i], b[i], i); } long put = n * (n - 1) * (n - 2) / 6; for(String line: datas.keySet()){ } for (String line : datas.keySet()) { int length = datas.get(line).size(); if (length <= 2) { } else { put -= length * (length - 1) * (length - 2) / 6; } } System.out.print(put + ""); } private void fAdd(int x, int y, int flag) { double K, B; for (int i = 0; i < flag; i++) { // Find out B = ? & K = ? B = x - a[i]; if (B == 0) { K = MAX_K; // B = x; } else { K = (y - b[i]) / B; B = y - K * x; } // Add New Line or Add New Point String id=K+"-"+B; if (datas.containsKey(id)) { HashSet<String> points = datas.get(id); points.add(x+"-"+y); } else { HashSet<String> points = new HashSet<String>(); points.add(a[i]+"-"+b[i]); points.add(x+"-"+y); datas.put(id, points); } } } }
No matter what, to use object as a key is totally wrong ! !
import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; public class Main { public int MAX_K = 1002; // The value k that stands for the max number public int n; public int[] a = new int[2005]; public int[] b = new int[2005]; // The position of each point private HashMap<Line, HashSet<Point>> datas; // The whole data that contains all lines and the corresponded points public static void main(String[] s) { Main context = new Main(); context.fMain(); } private void fMain() { Scanner sc = new Scanner(System.in); n = sc.nextInt(); a[0] = sc.nextInt(); b[0] = sc.nextInt(); datas = new HashMap<Line, HashSet<Point>>(); // HashSet<Point> points = new HashSet<Point>(); // HashSet will never allow its child to be the same. // Map will ensure all the keys will be the unique. for (int i = 1; i < n; i++) { a[i] = sc.nextInt(); b[i] = sc.nextInt(); fAdd(a[i], b[i], i); } long put = n * (n - 1) * (n - 2) / 6; for (Line line : datas.keySet()) { int length = datas.get(line).size(); if (length <= 2) { } else { put -= length * (length - 1) * (length - 2) / 6; } } System.out.print(put + ""); } private void fAdd(int x, int y, int flag) { double K, B; for (int i = 0; i < flag; i++) { // B = ? & K = ? B = x - a[i]; if (B == 0) { K = MAX_K; // B = x; } else { K = (y - b[i]) / B; B = y - K * x; } // Add New Line or Add New Point Line newLine = new Line(K, B); if (datas.containsKey(newLine)) { HashSet<Point> points = datas.get(newLine); points.add(new Point(x, y)); } else { HashSet<Point> points = new HashSet<Point>(); points.add(new Point(a[i], b[i])); points.add(new Point(x, y)); datas.put(newLine, points); } } } private class Line { public double k; public double b; public Line(double k, double b) { this.k = k; this.b = b; } } private class Point { public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; } } }
And C++ is better in this way
#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> using namespace std; struct node { int x, y; }; int n; //node a[100005]; node a[2005]; int main() { long long ans; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d%d", &a[i].x, &a[i].y); } ans = 0; for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { int x = a[i].x - a[j].x, y = a[i].y - a[j].y; for(int k = j + 1; k < n; k++) if(x * (a[k].y - a[i].y != y * (a[k].x - a[i].x))) ans++; } } printf("%I64d\n", ans); return 0; }
相关文章推荐
- 英语总结系列(七):Spring
- Java生成数据
- SpringMVC整合Shiro
- 自定义mybatis分页插件
- Java 数组类型(基本类型数组类型、封装类型数组类型、数组的数组类型)
- java 生成4位数的随机数据 70万条
- How to download offline javadoc?
- BASE64,MD5,SHA,HMAC加密與解密算法(java)
- Spring源码:Spring 容器资源加载模板模式应用
- Eclipse报错:java.lang.ClassNotFoundException: ContextLoaderListener
- java多线程
- SpringBoot 中使用 Log4j
- Java读取Unicode文件(UTF-8等)时碰到的BOM首字符问题,及处理方法
- 【Java】java中的compareTo和compare的区别
- JAVA 泛型的类型参数需要注意的地方
- java学习第一天
- SVN安装使用及eclipse整合SVN
- Java中一些误区,个人经验!
- Eclipse下如何打开ftl文件
- php开发环境安装配置(2)-eclipsephp