一、考察内容
30道单选题,3道编程题。单选题相关内容有计网、操作系统(Linux)、数据结构和排序算法、C相关知识等。
二、编程题一
题目内容:给一组含4个数(1-10)的数组,要求输出这四个数经过 加减乘除后 是否能组成二十四点,不能重复使用同一位置的数。
public static boolean Game24Points (int[] arr) {
// write code here
if(arr == null || arr.length == 0)
return false;
return dfs(arr,0,0);
}
public static boolean dfs(int[] arr,int cur,int target){
if (target == 24){
return true;
}
if (cur >= 4){
return false;
}
int curNum = arr[cur];
return dfs(arr,cur+1,target + curNum)
|| dfs(arr,cur+1,target - curNum)
|| dfs(arr,cur+1,target * curNum)
|| dfs(arr,cur+1,target / curNum);
}
public static void main(String[] args) {
int[] in = new int[]{7,2,1,10};
System.out.println(Game24Points(in));
}
三、编程题二
题目内容:有效的括号
https://leetcode-cn.com/problems/valid-parentheses/
public static boolean IsValidExp (String s) {
// write code here
if(s == null || s.length() == 0)
return true;
int length = s.length();
Stack<Character> stack = new Stack<>();
int i = 0;
while (i < length){
if (s.charAt(i) == '{' || s.charAt(i) == '[' || s.charAt(i) == '(' ){
stack.push(s.charAt(i));
}else{
if (!stack.isEmpty() && (s.charAt(i) == ')' && stack.peek() == '('
|| s.charAt(i) == '}' && stack.peek() == '{'
|| s.charAt(i) == ']' && stack.peek() == '[' )){
stack.pop();
}else {
return false;
}
}
i++;
}
return stack.isEmpty();
}
public static void main(String[] args) {
System.out.println(IsValidExp("{{{ "));
}
四、编程题三
题目内容:硬币有4种面值的,分别为1块、4块、16块、64块,纸币面值只有1024块面值的。现在小Y拿着1024块的纸币,买了一件商品花了N (1 <= N <= 1024) 元,请问最少能拿到多少枚硬币?
输入:N为200,输出最少能拿到17枚硬币(12枚64块,3枚16块,2枚4块)。
public static int GetCoinCount (int N) {
// write code here
int rest = 1024 - N;
int count = 0;
int[] mon = {1,4,16,64};
for (int i = mon.length - 1 ; i >= 0 ; i--){
int cur = rest / mon[i];
rest -= cur * mon[i];
count += cur;
}
return count;
}
public static void main(String[] args) {
System.out.println(GetCoinCount(200));
}