-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay95.java
More file actions
49 lines (40 loc) · 1.18 KB
/
Day95.java
File metadata and controls
49 lines (40 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.Scanner;
public class Day95 {
public static String nthTerm(int n) {
if (n == 1) {
return "1";
}
String currentTerm = "1";
for (int i = 2; i <= n; i++) {
currentTerm = nextTerm(currentTerm);
}
return currentTerm;
}
public static String nextTerm(String term) {
StringBuilder result = new StringBuilder();
int count = 1;
char currentChar = term.charAt(0);
for (int i = 1; i < term.length(); i++) {
if (term.charAt(i) == currentChar) {
count++;
} else {
result.append(count);
result.append(currentChar);
count = 1;
currentChar = term.charAt(i);
}
}
result.append(count);
result.append(currentChar);
return result.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
for (int t = 0; t < T; t++) {
int N = scanner.nextInt();
System.out.println(nthTerm(N));
}
scanner.close();
}
}