安卓的第一道编程题是求出怪兽的防御招式,可能带大招
笑哭,算法渣渣一枚,现在才完善,应该可以通过了
import java.util.Scanner; public class Main{ public static void main(String argv[]){ Scanner scanner=new Scanner(System.in); String input=scanner.nextLine(); System.out.println(getDefend(input)); } public static String getDefend(String in) { StringBuilder stringBuilder = new StringBuilder(); Node node = new Node(); for (int i = 0; i < in.length(); i++) { char item = in.charAt(i); if (node.isEmpty()) { node.addItem(item); } if (!node.isEmpty()) { if (node.isBig()) {//是大招,放C stringBuilder.append('C'); } else {//不是大招,放对应的小招 stringBuilder.append(node.getDefends()); } node.clear(); } else { if (i == in.length() - 1) { stringBuilder.append(node.getDefends()); } } } return stringBuilder.toString(); } public static char getD(char in) { char out = '\0'; switch (in) { case 'S': out = 'R'; break; case 'K': out = 'B'; break; case 'H': out = 'L'; break; } return out; } private static class Node { public char one = '\0'; public char two = '\0'; public char three = '\0'; public boolean isBig() { if (one != two && one != three && two != three) { return true; } return false; } public char[] getDefends() { char[] chars = new char[3]; chars[0] = getD(one); chars[1] = getD(two); chars[2] = getD(three); return chars; } public void clear() { one = '\0'; two = '\0'; three = '\0'; } public boolean isEmpty() { if ((one == '\0') || (two == '\0') || (three == '\0')) { return true; } return false; } public void addItem(char in) { if (one == '\0') { one = in; } else if (two == '\0') { two = in; } else if (three == '\0') { three = in; } } } }#百度#