728x90
https://swexpertacademy.com/main/solvingProblem/solvingProblem.do
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 1; tc <= t; tc++) {
int count = 0;
int a = sc.nextInt();
int b = sc.nextInt();
for (int i = a; i <= b; i++) {
String str = Integer.toString(i);
boolean isPossible = true;
int s = 0;
int e = str.length() - 1;
while (s <= e) {
if (str.charAt(s) != str.charAt(e)) {
isPossible = false;
break;
}
s++;
e--;
}
if (isPossible == false)
continue;
if (Math.sqrt(i) % 1 != 0) {
continue;
}
String str1 = Integer.toString((int) Math.sqrt(i));
s = 0;
e = str1.length() - 1;
while (s <= e) {
if (str1.charAt(s) != str1.charAt(e)) {
isPossible = false;
break;
}
s++;
e--;
}
if (isPossible == true) {
//System.out.println(i);
count++;
}
}
System.out.println("#"+tc+" "+count);
}
}
}
아래는 함수화한 코드
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 1; tc <= t; tc++) {
int count = 0;
int a = sc.nextInt();
int b = sc.nextInt();
for (int i = a; i <= b; i++) {
String str = Integer.toString(i);
if (Math.sqrt(i) % 1 != 0) {
continue;
}
boolean isPossible = true;
int s = 0;
int e = str.length() - 1;
isPossible = isPalindrome(str, s, e);
if (isPossible == false)
continue;
String str1 = Integer.toString((int) Math.sqrt(i));
s = 0;
e = str1.length() - 1;
isPossible = isPalindrome(str1, s, e);
if (isPossible == true) {
// System.out.println(i);
count++;
}
}
System.out.println("#" + tc + " " + count);
}
}
public static boolean isPalindrome(String str, int s, int e) {
while (s <= e) {
if (str.charAt(s) != str.charAt(e)) {
return false;
}
s++;
e--;
}
return true;
}
}
제곱근이 정수일 때 팰린드롬 검사를 실시했다.
728x90
'SW Expert Academy > SWEA D3' 카테고리의 다른 글
| [SW Expert Academy] [S/W 문제해결 기본] 4일차 - 거듭 제곱 (D3) (0) | 2024.08.28 |
|---|---|
| [SW Expert Academy] 장애물 경주 난이도 (D3) (0) | 2024.08.28 |
| [SW Expert Academy] 원재의 메모리 복구하기 (D3) (0) | 2024.08.27 |
| [SW Expert Academy] Calkin-Wilf tree 1 (D3) (0) | 2024.08.27 |
| [SW Expert Academy] 석찬이의 받아쓰기 (D3) (0) | 2024.08.27 |