首页 > 代码库 > 滴滴秋招笔试题(2016-09-18)
滴滴秋招笔试题(2016-09-18)
1.地下迷宫
这道题是网上找到别人的答案,拿过来学习学习,望勿怪。
import java.io.BufferedInputStream;import java.util.Scanner;public class 地下迷宫 { public static int[][] dir = { { 1, 0, 0 }, { 0, 1, 1 }, { -1, 0, 3 }, { 0, -1, 1 } }; public static int n = 0; public static int m = 0; public static int p = 0; public static int[][] min = new int[2][300]; public static int[][] curr = new int[2][300]; public static int mintime = Integer.MAX_VALUE; public static int currtime = 0; public static int steps = 0; public static int beststeps = 0; public static void main(String[] args) { Scanner scanner = new Scanner(new BufferedInputStream(System.in)); n = scanner.nextInt(); m = scanner.nextInt(); p = scanner.nextInt(); int[][] maze = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { maze[i][j] = scanner.nextInt(); } } scanner.close(); maze[0][0] = 0; go(0, 0, maze); if (mintime == Integer.MAX_VALUE) System.out.println("Can not escape!"); else { System.out.print("[" + min[0][0] + "," + min[1][0] + "]"); for (int i = 1; i < beststeps; i++) { System.out.print(",[" + min[0][i] + "," + min[1][i] + "]"); } System.out.print(",[" + 0 + "," + (m - 1) + "]"); } } public static void go(int a, int b, int[][] maze) { if (a == 0 && b == m - 1) { if (p >= currtime) { if (currtime < mintime) { mintime = currtime; min = curr.clone(); beststeps = steps; } } } for (int i = 0; i < 4; i++) { if (cango(a, b, dir[i][0], dir[i][1], maze)) { steps++; curr[0][steps] = a + dir[i][0]; curr[1][steps] = b + dir[i][1]; currtime += dir[i][2]; maze[a + dir[i][0]][b + dir[i][1]] = 0; go(a + dir[i][0], b + dir[i][1], maze); maze[a + dir[i][0]][b + dir[i][1]] = 1; currtime -= dir[i][2]; steps--; } } } public static boolean cango(int i, int j, int a, int b, int[][] maze) { if (i + a >= 0 && i + a < n && j + b >= 0 && j + b < m && maze[i + a][j + b] == 1 && p >= currtime) return true; return false; }}
2.末尾0的个数
输入一个正整数n,求n!末尾有多少个0;比如n=10;10!=3628800;所以答案为2;1<=n<=1000.
解析:我在做的时候没有注意到随着n的增大,阶数是特别大的,基本数据类型装不下,所有需要边做阶乘,边去计算末尾0的个数,然后把这个阶乘除以10.使其变小,以便能够存储下。
public class 末尾0的个数 { public static void main(String[] args) { int n = 100; int c = getCount(n); System.out.println(c); } private static int getCount(int n) { int sum = 1; int c = 0; for (int i = 1; i <= n; i++) { sum *= i; if (sum % 10 == 0) { c++; sum = sum / 10; } } return c; }}
滴滴秋招笔试题(2016-09-18)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。