1、回合攻擊問題

package com.hone.pdd; import java.util.Scanner; /** * 題目:模擬一個游戲場景,兩種傷害,一種正常傷害,一種是先蓄力(也算一個回合),在傷害,問:最多需要多少個回合敵人就會打倒? * 思路:-主要區分bufferedAttach和normalAttach之間的大小,注意是兩倍之間的大小區別。 * @author Xia * */ public class Test1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int hp = Integer.parseInt(in.nextLine()); int normalA = Integer.parseInt(in.nextLine()); int bufferedA = Integer.parseInt(in.nextLine()); int step = 0; // 回合數 if (bufferedA > normalA * 2) { step = hp/bufferedA; step = step * 2; //計算利用聚力之后的兩個回合的剩余血量 hp = hp - bufferedA * step/2; //如果剩余血量不等於0,並且剩余血量小於正常操作的消耗血量 if (hp != 0) { if (hp <= normalA) step++; else step += 2; } }else { step = hp/normalA; if (hp % normalA != 0 ) step++; } System.out.println(step); } }
Test2

package com.hone.pdd; import java.util.Scanner; /** * 利用一個二維數組來模擬整個的棋盤掉落情況。 * @author Xia * */ public class Test2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); char[][] samples = new char[12][12]; //定義一個二維數組來表示棋盤 for (int i = 0; i < n; i++) { samples[i] = in.next().toCharArray(); } //用一個二重的for循環來控制,但是外層的循環是以列為單位,遍歷內部的行單元元素 for (int j = 0; j < m; j++) { //列 for (int i = n-1; i >= 0; i--) { //行 if (samples[i][j] == '.') continue; if (samples[i][j] == 'x') continue; int k = i; while (k < n) { samples[k][j] = '.'; k++; if (k < n&&samples[k][j]!='.') { samples[k-1][j] = 'o'; break; } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { System.out.printf("%c",samples[i][j]); } System.out.println(); } } }
Test3

實際上求得是小數的循環結。
package com.hone.pdd; import java.util.HashMap; import java.util.Map; import java.util.Scanner; /** * 分類:1:可以整除的數 * 2:可以除盡的數(和第一種有重合) * 3:無限循環小數 * 思路:首先可以在循環的外部做一次除法得到一個余數。 * 其后在for循環中的得到的余數一定是小數點后面的數。 * 利用map來存儲小數點之后的數出現的位置。 * 無限循環小數出現的條件(余數出現重復) * @author Xia * */ public class Test3 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); int b = in.nextInt(); //用map來存儲小數點后面的數,及該數在小數點后面位置。 Map<Integer, Integer> map = new HashMap<Integer, Integer>(); a %= b; //固定把小數點后一位定為0位 for (int i = 0;; i++) { int x = a % b; if (x == 0) { System.out.println(i + " 0"); break; } Integer t = map.get(x); if (t != null) { System.out.printf("%d %d\n", t, i - t); break; } map.put(x, i); a = x * 10; } } }
Test4



這道題個人沒有理解,是參考考試完之后別人的答案。
package com.hone.pdd; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class Test4 { // n 表示單詞的個數 m表示單詞的長度 static int n; static int m; static List<Set<Character>> b; static Set<String> st; static boolean flag = false; public static void main(String[] args) { Scanner in = new Scanner(System.in); n = in.nextInt(); m = in.nextInt(); in.nextLine(); String[] s = new String[n]; st = new TreeSet<>(); for (int i = 0; i < n; i++) { s[i] = in.nextLine(); st.add(s[i]); } b = new ArrayList<>(); for (int i = 0; i < m; i++) { b.add(new TreeSet<>()); } for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { b.get(j).add(s[i].charAt(j)); } } // 深度優先搜索 dfs(0, new StringBuilder()); } private static void dfs(int k, StringBuilder sb) { if (flag) return; if (k == m) { if (!st.contains(sb.toString())) { System.out.println(sb); flag = true; } return; } //lambda表達式 b.get(k).forEach(character -> { StringBuilder ssb = new StringBuilder(sb); ssb.append(character); dfs(k + 1, ssb); }); } }
