商汤科技 8-19 java
第一题 AC 100% leetcode原题 package LinkCode; import java.util.Scanner; import java.util.Stack; public class SenseTime1 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); String str = sc.nextLine(); String[] temp = str.split(" "); System.out.println(cal(temp)); sc.close(); } private static int cal(String[] temp) { // TODO Auto-generated method stub int len = temp.length; if (len == 0) return 0; Stack<Integer> stack = new Stack<Integer>(); int num1, num2; for (int i = 0; i < len; i++) { if (temp[i].equals("+")) { num1 = stack.pop(); num2 = stack.pop(); stack.push(num2 + num1); } else if (temp[i].equals("-")) { num1 = stack.pop(); num2 = stack.pop(); stack.push(num2 - num1); } else if (temp[i].equals("*")) { num1 = stack.pop(); num2 = stack.pop(); stack.push(num2 * num1); } else if (temp[i].equals("/")) { num1 = stack.pop(); num2 = stack.pop(); stack.push(num2 / num1); } else { stack.push(Integer.parseInt(temp[i])); } } return stack.pop(); } }
第二题 AC 100% leetcode原题 package LinkCode; import java.util.Scanner; public class SenseTime2 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); System.out.println(cal(arr)); sc.close(); } private static int cal(int[] arr) { // TODO Auto-generated method stub int count1 = 0, count2 = 0, res = 0; for (int i = 0; i < arr.length - 1; i++) { if (arr[i] <= arr[i + 1]) { if (count2 > 0) { if (count1 > 0) { res = Math.max(res, count1 + count2 + 1); count1 = 0; } count2 = 0; } if (arr[i] < arr[i + 1]) { count1++; } else { count1 = 0; } } else if (arr[i] > arr[i + 1]) count2++; } if (count1 > 0 && count2 > 0) res = Math.max(res, count1 + count2 + 1); return res; } }
第三题 递归 AC 20% 数组越界,嗯没运行就知道会越界 方法不行 package LinkCode; import java.util.Scanner; public class SenseTime3 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); long n = sc.nextLong(); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int f = sc.nextInt(); System.out.println(fun(n, a, b, c, f)); sc.close(); } public static long fun(long n, int a, int b, int c, int f) { if (n < 0) return 0; else if (n == 0) return f; else { long temp = (long) (a * fun(n - 1, a, b, c, f) + b * fun(n - 2, a, b, c, f) + c * fun(n - 3, a, b, c, f) + 2 * Math.pow(n, 2) - n + 32767); return temp % 100000007; } } }
第四题 AC 66.67% package LinkCode; import java.util.Scanner; public class SenseTime4 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); System.out.println(cal(arr)); sc.close(); } private static int cal(int[] arr) { // TODO Auto-generated method stub int sum1 = 0, sum2 = 0, count1 = 0, count2 = 0; if (arr.length < 2 || arr.length == 0) return 0; for (int i = 0; i < arr.length - 2; i++) { sum1 = arr[i] + arr[i + 1]; sum2 = arr[i + 1] + arr[i + 2]; if (squnum(sum1) && squnum(sum2) && arr[i] != arr[i + 2])//6 10 15 15 10 6 count1 += 1; if (squnum(sum1) && squnum(sum2) && arr[i] == arr[i + 2])//6 10 6 count2 += 1; } return (count1 * 2 + count2); } private static boolean squnum(int sum) {// 完全平方数 // TODO Auto-generated method stub boolean flag = false; double temp = Math.sqrt(sum); int q = (int) temp; if (sum == Math.pow(q, 2)) flag = true; return flag; } }
#商汤科技##笔试题目#