import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner;    //(1,1),(2,2),(3,3)    3 //(0,0),(1,1),(1,-1)   2 public class Main {     //坐标点类     static class Point {         int x;         int y;            Point(int x, int y) {             this.x = x;             this.y = y;         }     }        public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         String str = sc.nextLine();         sc.close();         //解析坐标         String[] strs = str.split("\\),");         List<Point> list = new ArrayList<Point>();         for (int i = 0; i < strs.length; i++) {             strs[i] = strs[i].substring(1);             String[] ps = strs[i].split(",");             int x = Integer.parseInt(ps[0]);             if (i == strs.length - 1)                 ps[1] = ps[1].substring(0, ps[1].length() - 1);             int y = Integer.parseInt(ps[1]);             Point p = new Point(x, y);             list.add(p);         }         System.out.println(cal(list));     }            //计算     public static int cal(List<Point> list) {         if (list.size() == 0)             return 0;         if (list.size() == 1)             return 1;         int res = 0;                  for (int i = 0; i < list.size(); i++) {          //在leetcode上亲测在循环外定义HashMap并在循环内调用map.clear()比直接             //在循环内定义HashMap慢了2ms,内存少用了0.1m             Map<String, Integer> map = new HashMap<String, Integer>();                Point cur = list.get(i);                          //共线数             int g = 0;             //共点数             int gd = 1;             //共x数             int gx = 0;             //共y数             int gy = 0;             //共斜率数             int gk = 0;                for (int j = 0; j < list.size(); j++) {                 if (i == j)                     continue;                 Point temp = list.get(j);                 int x1 = cur.x - temp.x;                 int y1 = cur.y - temp.y;                 if (x1 == 0 && y1 == 0) {                     gd++;                     continue;                 }                 if (x1 == 0) {                     gx++;                     continue;                 }                 if (y1 == 0) {                     gy++;                     continue;                 }                 //由于直接算出double类型的斜率之后再进行比较会有误差                 //所以使用HashMap存储不同斜率值得数量                 int gc = gcd(x1, y1);                 x1 /= gc;                 y1 /= gc;                 String key = x1 + "/" + y1;                 if (!map.containsKey(key)) {                     map.put(key, 0);                 }                 int curGk = map.get(key) + 1;                 map.put(key, curGk);                 gk = Math.max(gk, curGk);             }             g = Math.max(gx, gy);             g = Math.max(g, gk);             res = Math.max(res, g + gd);         }         return res;     }           //求x、y最大公约数     public static int gcd(int x, int y) {         return y == 0 ? x : gcd(y, x % y);     }    }
点赞 2

相关推荐

小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
06-15 20:57
已编辑
门头沟学院 Java
CARLJOSEPH...:年轻人有傲气很正常,但是建议工作前洗净傲气。 说实在的,什么奖学金什么奖项的都很一般。尊重你的老师,在有时间的时候去上课,真遇到走不开的事,请态度端正地向你的老师说明情况,请求请假。我相信任何一个有师德的老师都会允许的(我的老师就是这样)。
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务