-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFizz Buzz
68 lines (58 loc) · 1.7 KB
/
Fizz Buzz
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Fizz Buzz
import java.io.*;
import java.util.*;
import java.util.ArrayList;
class StringArray {
public static String[] input(BufferedReader br, int n) throws IOException {
String[] s = br.readLine().trim().split(" ");
return s;
}
public static void print(String[] a) {
for(String e : a) {
System.out.print(e + " ");
}
System.out.println();
}
public static void print(ArrayList<String> a) {
for(String e : a) {
System.out.print(e + " ");
}
System.out.println();
}
}
class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-- > 0) {
int n;
n = Integer.parseInt(br.readLine());
Solution obj = new Solution();
ArrayList<String> res = obj.fizzBuzz(n);
StringArray.print(res);
System.out.println("~");
}
}
}
class Solution {
public static ArrayList<String> fizzBuzz(int n) {
ArrayList<String> result = new ArrayList<>();
HashMap<Integer, String> mp = new HashMap<>();
mp.put(3, "Fizz");
mp.put(5, "Buzz");
int[] divisors = { 3, 5 };
for (int i = 1; i <= n; i++) {
StringBuilder s = new StringBuilder();
for (int d : divisors) {
if (i % d == 0) {
s.append(mp.get(d));
}
}
if (s.length() == 0) {
s.append(i);
}
result.add(s.toString());
}
return result;
}
}