UVa 401 Palindromes

本题展现了JAVA字符处理的强大能力

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner cin = new Scanner(System.in);
    while (cin.hasNextLine()) {
      String s = cin.nextLine();
      String ss = new String(s);
      System.out.print(s);
      String s1 = new String(new StringBuffer(s).reverse().toString());
      boolean flag = true, flag1 = true;
      for (int i = 0; i < s.length(); i++) {
        if (s1.charAt(i) != s.charAt(i)) {
          flag = false;
          break;
        }
      }
      char[] a = s.toCharArray();
      String old = "ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678";
      String now = "A 3 HIL JM O 2TUVWXY 1SE Z 8";
      for (int i = 0; i < a.length; i++) {
        char ch = now.charAt(old.indexOf(a[i]));
        if (ch != ' ') {
          a[i] = ch;
        }else {
          flag1 = false;
          break;
        }
      }
      if (flag1) {
        s = String.valueOf(a);
        String s2 = new String(new StringBuffer(s).reverse().toString());
        for (int i = 0; i < ss.length(); i++) {
          if (s2.charAt(i) != ss.charAt(i)) {
            flag1 = false;
            break;
          }
        }
      }
      if (!flag && !flag1) System.out.println(" -- is not a palindrome.");
      if (flag && !flag1) System.out.println(" -- is a regular palindrome.");
      if (!flag && flag1) System.out.println(" -- is a mirrored string.");
      if (flag && flag1) System.out.println(" -- is a mirrored palindrome.");
      System.out.print('\n');
    }
  }
}

你可能感兴趣的:(UVa 401 Palindromes)