TL;DR

You can easily convert it to alphabet by casting the number to char

Practice

Concrete 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) + "");
}
}
}
``````

Concrete 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

ABC171C quoted from

Roger decided to keep all the 1000000000000001 dogs that suddenly appeared under him. The dogs were originally numbered from 1 to 1000000000000001, but Roger gave them names according to the following rules: Dogs with the numbers 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. The 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();

// Determine the character string from the back by converting the remainder divided by 26 into alphabet
while (n != 0) {
// // I don't fully understand why I need to do this here, so I would appreciate it if someone kindly tells me. ..
// I want to be converted to a when n %26 = 1 and ... to z when n %26 = 25,
// If this is the case, it will be b when n% 26 = 1, so we will do n--.
n--;
// loop1 18276
// loop2 701
// loop3 25

// Convert the number to char by casting it 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 character string was made in reverse order, reverse();
ans = ans.reverse();

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

``````

Postscript

I was so disappointed that I could solve the C problem. I will do my best next time.

Tags:

Updated: