알고리즘
Java) 회문 판별 알고리즘
Lee Jun Seo
2020. 4. 17. 00:10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class Main {
public static void main(String[] args) {
String a;
Scanner sc = new Scanner(System.in);
a = sc.nextLine();
int flag = 0;
char[] arr = new char [10000];
arr = a.toCharArray();
for(int i =0;i<a.length()/2;i++) {
if(arr[i] != arr[a.length()-i-1])
flag = 1;
}
if(flag == 0)
System.out.print("회문입니다.");
else
System.out.print("회문이 아닙니다.");
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
1. 문자열을 입력받아 배열로 바꾼다.
2. 문자열의 길이의 반만큼 for문을 돌린다.
3. flag를 0으로 맞춰준다.
4. 끝자리와 첫자리의 문자가 다르면 회문이 아니기 때문에 flag = 1 로 바꾼다.
5. flag를 통해서 회문을 판별한다.