工行提前批笔试第三道编程题

工行提前批笔试第三道编程题如何把x和y解析出来?#工商银行##笔试题目#
全部评论
scanf("(%d,%d)",&a[i],b[i]);为什么要当成字符串呢?
点赞 回复
分享
发布于 2019-10-14 14:16
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);     }    }
点赞 回复
分享
发布于 2019-10-14 17:23
博乐游戏
校招火热招聘中
官网直投
java的话字符串根据逗号split就行,然后循环每次+2
点赞 回复
分享
发布于 2019-10-14 12:14
c++挨个遍历匹配到‘(‘,然后把下一个和下三个分别赋值到Point(x,y),不知道你说的是不是这个意思
点赞 回复
分享
发布于 2019-10-14 12:25
第三题只把xy坐标解析出来了🤣
点赞 回复
分享
发布于 2019-10-14 12:28
C++ 可以了解下istreamstring,解决这种问题时很好用
点赞 回复
分享
发布于 2019-10-14 12:55
这有啥好解析的,固定格式的,只要遍历到')',那就取i-3和i-1位置的数不就行了。而且它那笔试好像看不了过了多少啊。顶多看一下它给的示例过没过。总之,工行的笔试,体验极差。还考vba,谁会那东西?
点赞 回复
分享
发布于 2019-10-14 13:09
点赞 回复
分享
发布于 2019-10-14 13:30
字符串录入,判断左括号之后判断负号,再把逗号之前的按位转换成int,逗号之后判断负号,再把右括号之前的按位转换成int。 想问问大家怎么看到AC的……不是只有保存么,看不到除了样例的其他测试用例和结果对错啊
点赞 回复
分享
发布于 2019-10-14 16:58
c++ 跟上面提到过的思路基本是一样的 找" ( ",然后找" , " 和 " ) ",然后根据位置和字符串算每个数字的值,多位数也考虑在内。 之后思路是每两个点求一条线,把所有线求出来之后看有多少一样的,然后根据线的数求点数
点赞 回复
分享
发布于 2019-10-15 21:04

相关推荐

2 7 评论
分享
牛客网
牛客企业服务