-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquareOrNot.java
More file actions
54 lines (48 loc) · 1.86 KB
/
SquareOrNot.java
File metadata and controls
54 lines (48 loc) · 1.86 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
49
50
51
52
53
54
package Divisions.Div3.Round970;
// code by senurah
public class SquareOrNot {
public static boolean isSquare(int num) {
double sqrt = Math.sqrt(num);
return ((sqrt - Math.floor(sqrt)) == 0) && (Math.pow((int)sqrt, 2) == num);
}
public static void main(String[] args) {
java.util.Scanner scanner = new java.util.Scanner(System.in);
int t = scanner.nextInt();
scanner.nextLine();
if(t >= 1 && t <= 10000) {
for(int i = 0; i < t; i++) {
int n = scanner.nextInt();
String s = scanner.next();
if(isSquare(n)) {
int size = (int)Math.sqrt(n);
boolean isBeautiful = true;
for(int row = 0; row < size; row++) {
for(int col = 0; col < size; col++) {
char currentChar = s.charAt(row * size + col);
if((row == 0 || row == size - 1 || col == 0 || col == size - 1)) {
if(currentChar != '1') {
isBeautiful = false;
break;
}
} else {
if(currentChar != '0') {
isBeautiful = false;
break;
}
}
}
if(!isBeautiful) break;
}
if(isBeautiful) {
System.out.println("Yes");
} else {
System.out.println("No");
}
} else {
System.out.println("No");
}
}
}
scanner.close();
}
}