网易2022秋季校园招聘-通用技术A卷-0918
生活随笔
收集整理的這篇文章主要介紹了
网易2022秋季校园招聘-通用技术A卷-0918
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
https://gitee.com/shentuzhigang/algorithm/tree/master/exam-netease/exam-netease-20210918
編程題
第一題
解決方案
JAVA
import java.util.Scanner;/*** @author ShenTuZhiGang* @version 1.0.0* @email 1600337300@qq.com* @date 2021-09-18 18:03*/ public class ExamNetEase2021091801 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();String str = String.valueOf(n);int count = 0;for (int i = 0; i < str.length(); i++) {if (str.charAt(i) != '0' && n % (str.charAt(i) - '0') == 0) {count++;}}System.out.println(count);} }第二題
解決方案
JAVA
71%
import java.util.Scanner;/*** @author ShenTuZhiGang* @version 1.0.0* @email 1600337300@qq.com* @date 2021-09-18 18:03*/ public class ExamNetEase2021091802 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);String str = scanner.nextLine();String[] strings = str.split(" ");str = strings[0];int n = Integer.parseInt(strings[1]);int[] a = new int[str.length() - 1];long count = 0;long sum = 0;long max = 0;for (int i = 1; i < str.length(); i++) {int i1 = Math.abs(str.charAt(i) - str.charAt(i - 1));a[i - 1] = Math.min(26 - i1, i1);count += a[i - 1];sum += a[i - 1];if (i - 1 - n >= 0) {count -= a[i - 1 - n];max = Math.max(max, count);}}if (a.length > n) {System.out.println(Math.min(sum + str.length() - max + n, sum + str.length()));} else {System.out.println(sum + str.length());}} }第三題
解決方案
JAVA
import java.util.Scanner;/*** @author ShenTuZhiGang* @version 1.0.0* @email 1600337300@qq.com* @date 2021-09-18 18:03*/ public class ExamNetEase2021091803 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);long n0 = scanner.nextLong();long n = n0;if (n == 0) {System.out.println(-1);return;}int count1 = 0, count2 = 0, num = 0;while (n != 0) {if (n % 2 == 1) {count1++;}num++;n /= 2;}long m = (long) (Math.pow(2, num) - n0);while (m != 0) {if (m % 2 == 1) {count2++;}m /= 2;}System.out.println(Math.min(count1, count2 + 1));} }第四題
解決方案
JAVA
版本一
import java.util.Comparator; import java.util.PriorityQueue; import java.util.Scanner;/*** @author ShenTuZhiGang* @version 1.0.0* @email 1600337300@qq.com* @date 2021-09-18 18:03*/ public class ExamNetEase2021091804 {private static int n;private static int a;private static int b;private static int[][] ans;private static boolean flag = false;private static int[][] pos = new int[2][2];private static char[][] map;public static void main(String[] args) {Scanner scanner = new Scanner(System.in);n = scanner.nextInt();a = scanner.nextInt();b = scanner.nextInt();map = new char[n][n];ans = new int[n][n];pos[0][0] = -1;pos[0][1] = -1;pos[1][0] = -1;pos[1][1] = -1;int p = 0;scanner.nextLine();for (int i = 0; i < n; i++) {map[i] = scanner.nextLine().toCharArray();for (int j = 0; j < n; j++) {if (map[i][j] == '*') {pos[p][0] = i;pos[p][1] = j;flag = true;}ans[i][j] = Integer.MAX_VALUE;}}PriorityQueue<Node> q = new PriorityQueue<>(new Comparator<Node>() {@Overridepublic int compare(Node o1, Node o2) {return o1.count - o2.count;}});Node node = new Node();node.x = 0;node.y = 0;node.count = 0;q.add(node);while (q.size() > 0) {node = q.poll();if (node.x >= n || node.y >= n || node.x < 0 || node.y < 0 || node.count > ans[n - 1][n - 1]) {continue;}if (ans[node.x][node.y] > node.count) {ans[node.x][node.y] = node.count;} else {return;}int c1 = 0;if (map[node.x][node.y] == '#') {c1 = a;} else if (map[node.x][node.y] == '*') {Node node1 = new Node();if (node.x == pos[0][0] && node.y == pos[0][1]) {node1.x = pos[1][0];node1.y = pos[1][1];} else {node1.x = pos[0][0];node1.y = pos[0][1];}node1.count = node.count + b;q.add(node1);}dfs(q, node.x + 1, node.y, node.count + c1);dfs(q, node.x, node.y + 1, node.count + c1);dfs(q, node.x - 1, node.y, node.count + c1);dfs(q, node.x, node.y - 1, node.count + c1);}System.out.println(ans[n - 1][n - 1]);}public static void dfs(PriorityQueue<Node> q, int x, int y, int count) {Node node1 = new Node();node1.x = x;node1.y = y;node1.count = count;q.add(node1);}static class Node {int x;int y;int count;} }版本二
超時
問答題
總結(jié)
以上是生活随笔為你收集整理的网易2022秋季校园招聘-通用技术A卷-0918的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客题霸 NC30 数组中未出现的最小正
- 下一篇: 《移动项目实践》实验报告——Androi