题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); char[] strchar = str.toCharArray(); int max = 1; for (int i = 0; i < strchar.length; i++) { int num = 0; for (int j = strchar.length - 1; j > i; j--) { if (strchar[i] == strchar[j]) { StringBuilder string = new StringBuilder(str.substring(i, j + 1)); StringBuilder string_rel = new StringBuilder(str.substring(i, j + 1)).reverse(); if (String.valueOf(string).equals(String.valueOf(string_rel))) { num = string.length(); if (num > max) { max = num; } break; } } } } System.out.print(max); } }