便利蜂java面试题_便利蜂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

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

if (tmpLength - commodities[i].getWidth() 

continue;

tmpLength -= commodities[i].getWidth();

maxValue += commodities[i].getValue();

}

return maxValue;

}

}

class Commodity implements Comparable {

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 

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 

for (int j = 0; 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 

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

int count = str.charAt(i) - '0';

char value = str.charAt(j);

for (int k=0; k

sb.append(value);

}

}

return sb.toString();

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值