ZZULIOJ 1155: String comparison multiple instances, Java

ZZULIOJ 1155: String comparison multiple instances, Java

Question description

Compare string sizes, but the comparison rules are different from the lexicographic rules. The new rules for character comparison are as follows: A < a < B < b < ………… < Z < z.

enter

The input data contains multiple test instances, each test instance occupies two lines, and each line has a string (containing only uppercase and lowercase letters, the length is less than 10000).

output

If the first string is less than the second string, output YES, otherwise, output NO.
Note: A < a < B < b < ………… < Z < z.

Sample inputCopy
abc
Bbc
Ab
a
ABcef
ABce
Sample outputCopy
YES
YES
NO
import java.io.*;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) throws IOException {
    
    
        Scanner sc = new Scanner(System.in);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        while (sc.hasNext()) {
    
    
            char[] a = sc.next().toCharArray();
            char[] b = sc.next().toCharArray();
            for (int i = 0; i < a.length; i++) {
    
    
                if (a[i] >= 'a') a[i] = (char) ((a[i] - 'a') * 2 + 1);
                else a[i] = (char) ((a[i] - 'A') * 2);
            }
            for (int i = 0; i < b.length; i++) {
    
    
                if (b[i] >= 'a') b[i] = (char) ((b[i] - 'a') * 2 + 1);
                else b[i] = (char) ((b[i] - 'A') * 2);
            }
            int ok = String.valueOf(a).compareTo(String.valueOf(b));
            if (ok < 0) bw.write("YES\n");
            else bw.write("NO\n");
            bw.flush();
        }
        bw.close();
    }
}

Guess you like

Origin blog.csdn.net/qq_52792570/article/details/132609549