概述
第一题是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();
}
}
最后
以上就是迅速黑米为你收集整理的便利蜂java面试题_便利蜂20200507笔试题(Java)的全部内容,希望文章能够帮你解决便利蜂java面试题_便利蜂20200507笔试题(Java)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复