這道題是網上找到別人的答案,拿過來學習學習,望勿怪。java
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.spa
解析:我在作的時候沒有注意到隨着n的增大,階數是特別大的,基本數據類型裝不下,全部須要邊作階乘,邊去計算末尾0的個數,而後把這個階乘除以10.使其變小,以便可以存儲下。code
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; } }