微众java岗笔试题解

三角形数字

直接找出公式,套就完事

package com.weizhong;
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Main1 {


    /*请完成下面这个函数,实现题目要求的功能
    当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^
    ******************************开始写代码******************************/
    static String GetResult(int N) {
        int s = 0;
        for (int i = 1; i < 1000000; i++) {
            s += i;
            if (s >= N) {
                int total = i + 1;
                int sub = 0;
                if ( i % 2 == 0) {
                    sub = i - (s-N);
                } else {
                    sub = s - N + 1;
                }
                return String.valueOf(sub) + "/" + String.valueOf(total- sub);
            }
        }
        return null;
    }
    /******************************结束写代码******************************/


    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String res;

        int _N;
        _N = Integer.parseInt(in.nextLine().trim());

        res = GetResult(_N);
        System.out.println(res);
    }
}

正方形

笨法子,从6到1放
654都是1个正方形一部车,5塞满1,4先塞2,没满再塞1
3复杂些,用最后一部车的剩余空间来塞2跟1,也是先塞2再塞1
2的最后一部车空位塞1
1直接塞

package com.weizhong;

import java.util.Scanner;

public class Main2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] arr = new int[6];
        for (int i = 0; i < 6; i++) {
            arr[i] = in.nextInt();
        }
        int ret = 0;

        if (arr[5] > 0) {
            ret += arr[5];
        }
        if (arr[4] > 0) {
            ret += arr[4];
            arr[0] -= 11*arr[4];
        }
        if (arr[3] > 0) {
            ret += arr[3];
            if (arr[1] >= 5* arr[3]) {
                arr[1] -= 5* arr[3];
            } else {
                arr[0] -= 4*(5*arr[3] - arr[1]);
                arr[1] = 0;
            }
        }
        if (arr[2] > 0) {
            if (arr[2] % 4 == 0) {
                ret += arr[2] / 4;
            }
            else {
                ret += arr[2] / 4 + 1;
                int s = arr[2] % 4;
                if (s == 1) {
                    if (arr[1] >= 5) {
                        arr[1] -= 5;
                        arr[0] -= 7;
                    } else {
                        arr[0] -= 27-4*arr[1];
                        arr[1] = 0;
                    }
                } else if (s == 2) {
                    if (arr[1] >= 3) {
                        arr[1] -= 3;
                        arr[0] -= 6;
                    } else {
                        arr[0] -= 18 - 4*arr[1];
                        arr[1] = 0;
                    }
                } else {
                    if (arr[1] >= 1) {
                        arr[1] -= 1;
                        arr[0] -= 5;
                    } else {
                        arr[0] -= 9;
                    }
                }
            }

        }
        if (arr[1] > 0) {
            if (arr[1] % 9 == 0) {
                ret += arr[1] / 9;
            }
            else {
                ret += arr[1] / 9 + 1;
                arr[0] -= (arr[1] % 9) * 4;
            }
        }
        if (arr[0] > 0) {
            if (arr[0] % 36 == 0) ret += arr[0] / 36;
            else ret += arr[0] / 36 + 1;
        }
        System.out.println(ret);
    }

}

膨胀圆

这题复杂一点,重点是把圆抽象出一个类,然后再计算,思路会清晰些
过程:
初始每个圆的半径都为0
对所有圆两两求相遇时间(这里注意如果有一个是停止膨胀的圆,时间要x2)
找到最早相遇的两个圆,以及相遇时间,将这两个圆停止膨胀
更新所有圆的半径

重复上述过程,直到所有圆都停止膨胀

复杂度O(n^3),不知道有没有更好的算法
也许可以往图的方向去想
package com.weizhong;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main3 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        List<Node> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            float x = in.nextFloat();
            float y = in.nextFloat();
            list.add(new Node(x, y));
        }

        int running = list.size();
        while (running > 0) {
            float minDis = Float.MAX_VALUE;
            int a = -1;
            int b = -1;
            for (int i = 0; i < list.size() - 1; i++) {
                for (int j = i + 1; j < list.size(); j++) {
                    float dis = Node.timeToMeet(list.get(i), list.get(j));
                    if (dis < minDis) {
                        minDis = dis;
                        a = i;
                        b = j;
                    }
                }
            }
            for (int i = 0; i < list.size(); i++) {
                Node node = list.get(i);
                if (!node.isStop) node.setR(node.getR() + minDis);
            }

            Node n1 = list.get(a);
            Node n2 = list.get(b);
            if (!n1.isStop) {
                n1.setStop(true);
                running--;
            }
            if (!n2.isStop) {
                n2.setStop(true);
                running--;
            }

        }
        for (int i = 0; i < list.size() -1; i++) {
            System.out.printf("%.3f ", list.get(i).getR());
        }
        System.out.printf("%.3f", list.get(list.size()-1).getR());
    }

}

class Node {
    float x;
    float y;
    float r;
    boolean isStop;

    public Node(float x, float y) {
        this.x = x;
        this.y = y;
        r = 0;
        isStop = false;
    }

    public static float distance(Node n1, Node n2) {
        return (float)Math.sqrt((n1.x - n2.x)* (n1.x - n2.x) + (n1.y -n2.y) * (n1.y - n2.y));
    }

    public static float timeToMeet(Node n1, Node n2) {
        if (n1.isStop() && n2.isStop()) {
            return Float.MAX_VALUE;
        } else if (n1.isStop() || n2.isStop) {
            return (distance(n1, n2) - n1.r - n2.r);
        } else {
            return (distance(n1, n2) - n1.r - n2.r) / 2;
        }
    }

    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    public float getR() {
        return r;
    }

    public void setR(float r) {
        this.r = r;
    }

    public boolean isStop() {
        return isStop;
    }

    public void setStop(boolean stop) {
        isStop = stop;
    }
}
#题解##笔试题目#
全部评论
大佬第一题公式是啥呀
点赞 回复 分享
发布于 2018-09-18 20:14
膜拜大佬,第三题题目还没看懂😂
点赞 回复 分享
发布于 2018-09-18 19:09
第三题也想到了这个思路,来不及写了😂
点赞 回复 分享
发布于 2018-09-18 18:39
第三题因为把取平方根算成了绝对值还看不出来来不及测试。。。有什么数据可以测试一下吗?或者老哥们能看下有什么问题吗。 import java.util.ArrayList;         import java.util.Collections;         import java.util.Scanner; public class Main{     public static void main(String[] args) {         Scanner in = new Scanner(System.in);         int n = in.nextInt();         int[] x = new int[n];         int[] y = new int[n];         for(int i = 0; i < n; i++){             x[i] = in.nextInt();             y[i] = in.nextInt();         }         int[][] distance = new int[n][n];         ArrayList<Point> list = new ArrayList<>();         for(int i = 0; i < n; i++){             for(int j = i + 1; j < n; j++){                 int a = x[i]-x[j];                 int b = y[i] - y[j];                 double d = Math.pow(Math.pow((double)a,2) + Math.pow((double)b,2),0.5);                 Point point = new Point(i,j,d );                 list.add(point);             }         }         for(int i = 0; i<n;i++){             Point p = list.get(i);         }         //保存半径,初始化-1         double[] len = new double[n];         for(int i = 0; i<len.length;i++) len[i] = -1;         Collections.sort(list, (o1, o2) -> Double.compare(o1.distance,o2.distance));         //从最短看是更新他们的半径。         for(int i = 0; i < n; i++){             Point p = list.get(i);             if(len[p.i] >0) len[p.j] = p.distance-p.i;             else if(len[p.j] >0) len[p.i] = p.distance-p.j;             else {                 len[p.i] = p.distance/2;                 len[p.j] = p.distance/2;             }         }         for(double d:len){             System.out.print(String .format("%.3f",d)+ " ");         }     } } class Point{     int i;     int j;     double distance;     public Point(int i, int j ,double distance){         this.i = i;         this.j = j;         this.distance = distance;     } }
点赞 回复 分享
发布于 2018-09-18 18:37
膜拜一下大佬 第三题根本无从下手。。😢
点赞 回复 分享
发布于 2018-09-18 18:28
太秀了
点赞 回复 分享
发布于 2018-09-18 18:25

相关推荐

牛客583549203号:腾讯还好,况且实习而已,实习生流动性很大,属于正常现象,记得和HR委婉解释
点赞 评论 收藏
分享
评论
点赞
20
分享

创作者周榜

更多
牛客网
牛客企业服务