(Small story) Numerical value → simple conversion method of alphabet (Java)

TL;DR You can easily convert it to the alphabet by casting the number to char.

Practice

Specific code 1

Main.java


public class Main {
    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 26; i++) {
            System.out.print((char) (97 + i) + " ");
        }
    }
}

Specific code 2

Main.java


public class Main {
    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 26; i++) {
            //You can also write like this**
            System.out.print((char) ('a' + i) + " ");
        }
    }
}

Output result

a b c d e f g h i j k l m n o p q r s t u v w x y z

application

Quoted from ABC171C

Roger decided to keep all the 1000000000000001 dogs that suddenly appeared to him. The dogs were originally numbered from 1 to 1000000000000001, but Roger gave them names according to the following rules: Dogs numbered 1,2, ⋯, 26 are named a, b, ..., z in that order. Dogs numbered 27,28,29, ⋯, 701, 702 are named aa, ab, ac, ..., zy, zz in that order. Dogs numbered 703,704,705, ⋯, 18277,18278 are named aaa, aab, aac, ..., zzy, zzz in that order. Dogs numbered 18279,18280,18281, ⋯, 475253,475254 are named aaaa, aaab, aaac, ..., zzzy, zzzz in that order. Dogs numbered 475255,475256, ⋯ are named aaaaa, aaaab, ... in that order. (Omitted below)

Main.java


import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);

        long n = sc.nextLong(); //Example: 18277
        StringBuilder ans = new StringBuilder();

        //By converting the remainder divided by 26 to the alphabet, the character string is determined from the back
        while (n != 0) {
            // //Why n here--I don't fully understand what I need to do, so I would appreciate it if someone kindly could tell me. ..
            // 2020/06/22 Addendum
            // n % 26 =At 1 a,... n % 26 =I want it to be converted to z at 25,
            //At this rate n% 26 =Since it becomes b when it is 1, n--To do.
            n--;
            // loop1 18276
            // loop2 701
            // loop3 25

            //Convert to alphabet by casting numbers to char
            ans.append((char) ('a' + n % 26));
            // loop1 18276 % 26 = 24 => y 
            // loop2 701 % 26 = 25 => z 
            // loop3 25 % 26 = 25 => z 

            n /= 26;
            // loop1 702 
            // loop2 26
            // loop3 0 ->End
        }

     //Since the strings were created in reverse order, reverse();
        ans = ans.reverse();

        System.out.println(ans); // zzy
        sc.close();
    }
}


Afterword

I was disappointed that I couldn't solve the C problem. I will do my best next time.

Recommended Posts

(Small story) Numerical value → simple conversion method of alphabet (Java)
Benefits of Java static method
[Java beginner] Conversion from character string to numerical value-What is the parseInt method of the Integer class? ~
Numerical value (int) ⇔ character string (String) conversion
[Java version] The story of serialization
Story of passing Java Gold SE8
[Java small story] Monitor when a value is added to the List
Story of paiza.jp [Java standard input solution]
[Basic knowledge of Java] About type conversion
The story of writing Java in Emacs