1、Api运用题(日历Calendar)
题目:有邪教称1999年12月31日是世界末日,当然谣言已经不攻自破。还有人称今后的某个世纪末的12月31日,如果是星期一则会…有趣的是,任何一个世纪末的年份的12月31日都不可能是星期一!!!于是"谣言制造商"又修改为星期日…
1999年12月31日是星期五,请问:未来哪一个离我们最近的一个世纪末年(即XX99年)的12月31日正好是星期天(即星期日)?
回答年份即可
package action; import java.util.Calendar; public class demo3 { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); for (int i = 1999; i < 10000; i += 100) { calendar.set(Calendar.YEAR, i); calendar.set(Calendar.MONTH, 11); calendar.set(Calendar.DATE, 31); if (calendar.get(Calendar.DAY_OF_WEEK) == 1) { System.out.println(i); break; } } } }
2、星辰大海
最新的火星探测机器人curiosity被困在了一个二维迷宫里,迷宫由一个个方格组成。
共有四种方格:
‘.’ 代表空地,curiosity可以穿过它
‘#’ 代表障碍物,不可穿越,不可停留
‘S’ 代表curiosity的起始位置
‘T’ 代表curiosity的目的地
NASA将会发送一系列的命令给curiosity,格式如下:“LRUD”分别代表向左,向右,向上,向下走一步。由于地球和火星之间最近时也有55000000km!所以我们必须提前判断这一系列的指令会让curiosity最终处在什么样的状态,请编程完成它。
输入格式
第一行是一个整数T,代表有几个测试样例
每个测试样例第一行是一个整数N(1<=N<=50))代表迷宫的大小(N*N)。随后的N行每行由N个字符串组成,代表迷宫。接下来的一行是一个整数Q,代表有多少次询问,接下来的Q行每行是一个仅由“LRUD”四个字母的组成的字符串,字符转长度小于1000.
输出格式
对于每个询问输出单独的一行:
“I get there!”:执行给出的命令后curiosity最终到达了终点。
“I have no idea!”:执行给出的命令后curiosity未能到达终点。
“I am dizzy!”:curiosity在执行命令的过程中撞到了障碍物。
“I am out!”:代表curiosity在执行命令的过程中走出了迷宫的边界。
输入示例:
2 2 S. #T 2 RD DR 3 S.# .#. .T# 3 RL DDD DDRR
输出示例:
I get there! I am dizzy! I have no idea! I am out! I get there!
我的这个写法动脑少一些,推荐用搜索的方式编写。
package action; import java.util.Scanner; public class demo3 { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { String[] arr = new String[999]; int scount = 0; int a = sc.nextInt(); for (int i = 0; i < a; i++) { int b = sc.nextInt(); char[][] bb = new char[b][b]; for (int j = 0; j < b; j++) { bb[j] = sc.next().toCharArray(); } int c = sc.nextInt(); String[] cc = new String[c]; for (int j = 0; j < cc.length; j++) { cc[j] = sc.next(); } char[][] ccc = new char[c][1000]; for (int j = 0; j < c; j++) { for (int j2 = 0; j2 < cc[j].length(); j2++) { ccc[j][j2] = cc[j].charAt(j2); } } for (int j = 0; j < c; j++) { int h = 0; int s = 0; zb: for (int j2 = 0; j2 < 1000; j2++) { zc: for (int k = 0; k < b; k++) { for (int k2 = 0; k2 < b; k2++) { if (j2 == 0 && bb[k][k2] == 'S') { h = k2; s = k; break zc; } } } if (ccc[j][j2] >= 'A' & ccc[j][j2] <= 'Z') { if (ccc[j][j2] == 'L') { h--; if (s < 0 | s == b | h < 0 | h == b) { arr[scount++] = "走出了迷宫的边界!"; break zb; } else if (bb[s][h] == '#') { arr[scount++] = "执行命令的过程中撞到了障碍物导致眩晕!"; break zb; } else if (bb[s][h] == 'T') { arr[scount++] = "抵达终点!"; break zb; } } else if (ccc[j][j2] == 'R') { h++; if (s < 0 | s == b | h < 0 | h == b) { arr[scount++] = "走出了迷宫的边界!"; break zb; } else if (bb[s][h] == '#') { arr[scount++] = "执行命令的过程中撞到了障碍物导致眩晕!"; break zb; } else if (bb[s][h] == 'T') { arr[scount++] = "抵达终点!"; break zb; } } else if (ccc[j][j2] == 'U') { s--; if (s < 0 | s == b | h < 0 | h == b) { arr[scount++] = "走出了迷宫的边界!"; break zb; } else if (bb[s][h] == '#') { arr[scount++] = "执行命令的过程中撞到了障碍物导致眩晕!"; break zb; } else if (bb[s][h] == 'T') { arr[scount++] = "抵达终点!"; break zb; } } else if (ccc[j][j2] == 'D') { s++; if (s < 0 | s == b | h < 0 | h == b) { arr[scount++] = "走出了迷宫的边界!"; break zb; } else if (bb[s][h] == '#') { arr[scount++] = "执行命令的过程中撞到了障碍物导致眩晕!"; break zb; } else if (bb[s][h] == 'T') { arr[scount++] = "抵达终点!"; break zb; } } } else { arr[scount++] = "老师付也不知道路了啊,游戏结束!"; break zb; } } } } for (int i = 0; i < scount; i++) { System.out.println(arr[i]); } } }
写一个【2048】·强化处理边缘值(虽然不难,代码多,相当于6个题)
package action; import java.io.IOException; import java.util.*; /** * java控制台2048 2022年4月3日09:35:17 * 上W左A下S右D */ public class demo { public static final int X = 4; public static final int Y = 4; public static int model[][] = new int[X][Y]; public static int step = 0; public static boolean gameover = false; public static int enumM[] = { 2, 2, 2, 2, 4, 4, 4, 8 }; public static void main(String[] args){ Random ra=new Random(); int randomX, randomY; randomX = ra.nextInt(X); randomY = ra.nextInt(model[randomX].length); model[randomX][randomY] = 2; randomX = ra.nextInt(X); randomY = ra.nextInt(model[randomX].length); model[randomX][randomY] = 2; randomX = ra.nextInt(X); randomY = ra.nextInt(model[randomX].length); model[randomX][randomY] = 4; randomX = ra.nextInt(X); randomY = ra.nextInt(model[randomX].length); model[randomX][randomY] = 2048; outPrint(); while (!gameover) { int read; try { read = System.in.read(); change(read); } catch (IOException e) { e.printStackTrace(); } } } public static void outPrint() { for (int i = 0; i < X; i++) { for (int j = 0; j < Y; j++) { System.out.print("【"); if (model[i][j] == 0) { System.out.print(" "); } if (model[i][j] > 0 && model[i][j] < 9) { System.out.print(" " + model[i][j] + " "); } if (model[i][j] > 9 && model[i][j] < 100) { System.out.print(" " + model[i][j] + " "); } if (model[i][j] > 99 && model[i][j] < 1000) { System.out.print(" " + model[i][j]); } if (model[i][j] > 999) { System.out.print(model[i][j]); } System.out.print("】"); } System.out.println(); } } public static void change(int dir) { switch (dir) { case 115:// S 下 int[] xp4 = new int[Y]; for (int i = 0; i < Y; i++) { boolean goon = true; while (goon) { int[] temp = new int[X]; int tempIdex = X - 1; for (int j = X - 1; j >= 0; j--) { if (model[j][i] != 0) { temp[tempIdex--] = model[j][i]; } } boolean hv = false; for (int j = X - 1; j > 0; j--) { if (temp[j] == temp[j - 1] && temp[j] != 0) { temp[j] = temp[j] * 2; temp[j - 1] = 0; hv = true; } } goon = hv; int is0 = 0; for (int j = X - 1; j >= 0; j--) { model[j][i] = temp[j]; if (temp[j] == 0) is0++; } if (is0 > 0) { xp4[i] = 1;// 可插牌 } } } // 插牌 List<Integer> space4 = new ArrayList<Integer>(); for (int j = 0; j < xp4.length; j++) { if (xp4[j] == 1) { space4.add(j); } } if (space4.size() == 0) { gameover = true; System.out.println("game over"); System.exit(0); } else { int a = (int) (Math.random() * (space4.size())); Integer index = space4.get(a); for (int j = X - 1; j >= 0; j--) { if (model[j][index] == 0) { model[j][index] = enumM[(int) (Math.random() * enumM.length)]; break; } } } outPrint(); break; case 100:// D 右 int[] xp = new int[X]; for (int i = 0; i < X; i++) { boolean goon = true; while (goon) { int[] temp = new int[Y]; int tempIdex = Y - 1; // 去空 for (int j = Y - 1; j >= 0; j--) { if (model[i][j] != 0) { temp[tempIdex--] = model[i][j]; } } boolean hv = false; // 合并 for (int j = 0; j < Y - 1; j++) { if (temp[j] == temp[j + 1] && temp[j] != 0) { temp[j] = temp[j] * 2; temp[j + 1] = 0; hv = true; } } goon = hv; int is0 = 0; for (int j = 0; j < Y; j++) { model[i][j] = temp[j]; if (temp[j] == 0) is0++; } if (is0 > 0) { xp[i] = 1;// 可插牌 } } } // 插牌 List<Integer> space = new ArrayList<Integer>(); for (int j = 0; j < xp.length; j++) { if (xp[j] == 1) { space.add(j); } } if (space.size() == 0) { gameover = true; System.out.println("game over"); System.exit(0); } else { int a = (int) (Math.random() * (space.size())); Integer index = space.get(a); for (int j = Y - 1; j >= 0; j--) { if (model[index][j] == 0) { model[index][j] = enumM[(int) (Math.random() * enumM.length)]; break; } } } outPrint(); break; case 119:// W 上 int[] xp3 = new int[Y]; for (int i = 0; i < Y; i++) { boolean goon = true; while (goon) { int[] temp = new int[X]; int tempIdex = 0; for (int j = 0; j < X; j++) { if (model[j][i] != 0) { temp[tempIdex++] = model[j][i]; } } boolean hv = false; for (int j = 0; j < X - 1; j++) { if (temp[j] == temp[j + 1] && temp[j] != 0) { temp[j] = temp[j] * 2; temp[j + 1] = 0; hv = true; } } goon = hv; int is0 = 0; for (int j = 0; j < X; j++) { model[j][i] = temp[j]; if (temp[j] == 0) is0++; } if (is0 > 0) { xp3[i] = 1;// 可插牌 } } } // 插牌 List<Integer> space3 = new ArrayList<Integer>(); for (int j = 0; j < xp3.length; j++) { if (xp3[j] == 1) { space3.add(j); } } if (space3.size() == 0) { gameover = true; System.out.println("game over"); System.exit(0); } else { int a = (int) (Math.random() * (space3.size())); Integer index = space3.get(a); for (int j = 0; j < X; j++) { if (model[j][index] == 0) { model[j][index] = enumM[(int) (Math.random() * enumM.length)]; break; } } } outPrint(); break; case 97:// A 左 int[] xp2 = new int[X]; for (int i = 0; i < X; i++) { boolean goon = true; while (goon) { int[] temp = new int[Y]; int tempIdex = 0; for (int j = 0; j < Y; j++) { if (model[i][j] != 0) { temp[tempIdex++] = model[i][j]; } } boolean hv = false; for (int j = 0; j < Y - 1; j++) { if (temp[j] == temp[j + 1] && temp[j] != 0) { temp[j] = temp[j] * 2; temp[j + 1] = 0; hv = true; } } goon = hv; int is0 = 0; for (int j = 0; j < Y; j++) { model[i][j] = temp[j]; if (temp[j] == 0) is0++; } if (is0 > 0) { xp2[i] = 1;// 可插牌 } } } // 插牌 List<Integer> space2 = new ArrayList<Integer>(); for (int j = 0; j < xp2.length; j++) { if (xp2[j] == 1) { space2.add(j); } } if (space2.size() == 0) { gameover = true; System.out.println("game over"); System.exit(0); } else { int a = (int) (Math.random() * (space2.size())); Integer index = space2.get(a); for (int j = 0; j < Y; j++) { if (model[index][j] == 0) { model[index][j] = enumM[(int) (Math.random() * enumM.length)]; break; } } } outPrint(); break; default: break; } } }