滴滴秋招笔试题(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;
    }

}
原文地址:https://www.cnblogs.com/wangjiangwu/p/5882834.html