便利蜂20200507笔试题(Java)


第一题是0-1背包问题, 用贪心算法解, 通过14%
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Main4 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int length = Integer.parseInt(sc.nextLine());
        String [] widthsStr = sc.nextLine().split(",");
        String [] valuesStr = sc.nextLine().split(",");
        sc.close();

        int n = widthsStr.length;
        Commodity [] commodities = new Commodity[n];
        for (int i=0; i<n; i++) {
            Commodity commodity = new Commodity(Integer.parseInt(widthsStr[i]), Integer.parseInt(valuesStr[i]));
            commodities[i] = commodity;
        }
        // 按单位价值从大到小排序
        Arrays.sort(commodities, Collections.reverseOrder());

        int ans = solve(commodities, n, length);
        System.out.println(ans);
    }

    private static int solve(Commodity [] commodities, int n, int length) {
        int tmpLength = length;
        int maxValue = 0;
        for (int i=0; i<n; i++) {
            if (tmpLength - commodities[i].getWidth() < 0)
                continue;
            tmpLength -= commodities[i].getWidth();
            maxValue += commodities[i].getValue();
        }
        return maxValue;
    }
}

class Commodity implements Comparable<Commodity> {
    private double width;
    private double value;
    private double unitValue;

    public Commodity(double width, double value) {
        this.width = width;
        this.value = value;
        this.unitValue = (width == 0) ? 0 : value / width;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public double getUnitValue() {
        return unitValue;
    }

    public void setUnitValue(double unitValue) {
        this.unitValue = unitValue;
    } 

    @Override
    public int compareTo(Commodity commodity) {
        double value = commodity.unitValue;
        if (unitValue > value)
            return 1;
        if (unitValue < value)
            return -1;
        return 0;
    }
}
第二题是给一个图像, 问给定的字符串能否在图像中连续
import java.util.Scanner;

public class Main2 {

    static char[][] photo = {
            {'0', '1', 'C', 'H', 'A'},
            {'9', 'E', '7', 'B', 'I'},
            {'K', 'D', '4', '8', 'J'},
            {'6', '5', 'F', 'G', 'O'},
            {'L', 'N', 'M', '2', '3'}};

    static int dx[] = {-1, 0, 1, 0};
    static int dy[] = {0, 1, 0, -1};

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean flag;
        String str;
        while (sc.hasNext()) {
            str = sc.nextLine();
            flag = isLianxu(str);
            if (flag == false) {
                System.out.println("N");
            } else {
                System.out.println("Y");
            }
        }
        sc.close();
    }

    private static boolean isLianxu(String str) {
        char c = str.charAt(0);
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (c == photo[i][j]) {
                     return dfs(str, 1, i, j);
                }
            }
        }
        return false;
    }

    private static boolean dfs(String str, int index, int i, int j) {
        if (str.length() == index) {
            return true;
        }
        for (int d = 0; d < 4; d++) {
            int x = i + dx[d], y = j + dy[d];
            if (isSafe(x, y) && str.charAt(index)==photo[x][y]) {
                return dfs(str, index+1, x, y);
            }
        }
        return false;
    }

    private static boolean isSafe(int i, int j) {
        if (i >= 0 && i <= 4 && j >= 0 && j <= 4) {
            return true;
        }
        return false;
    }
}
第三题, 字符串解码
import java.util.Scanner;

public class Main1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        sc.close();
        String ans = method(str);
        System.out.println(ans);
    }

    private static String method(String str) {
        StringBuffer sb = new StringBuffer();
        for (int i=0, j=1; j<str.length(); i=i+2, j=j+2) {
            int count = str.charAt(i) - '0';
            char value = str.charAt(j);
            for (int k=0; k<count; k++) {
                sb.append(value);
            }
        }
        return sb.toString();
    }
}

#便利蜂笔试##笔试题目##便利蜂#
全部评论
我的第一题也是过了14%,想了好久也没解决,感觉就是很简单的01背包问题啊😓
点赞 回复
分享
发布于 2020-05-07 21:19
我第一题同14%,我在想是不是输入有问题,我直接输出34都有20多通过率
点赞 回复
分享
发布于 2020-05-07 22:05
联易融
校招火热招聘中
官网直投
第一题真的奇怪,我也是卡了百分之14,改long也不行
点赞 回复
分享
发布于 2020-05-08 00:14

相关推荐

点赞 评论 收藏
转发
1 10 评论
分享
牛客网
牛客企业服务