본문 바로가기
수업시간 JAVA/문제

회문/ 팰린드롬

by SEOKIHOUSE 2023. 8. 10.

package hkhkhkhk;

import java.util.ArrayList;
import java.util.List;

public class hello {

	public static void main(String[] args) {
		int num = 112;
		String stNum = Integer.toString(num);
		
		List<Integer> list = test(1,110000);
		System.out.println(list);
	}
	
	//1~100
	public static List<Integer> test(int n, int m) {
		List<Integer> list = new ArrayList<>();
		
		for(int i = n; i<m; i++) {
			String stNum = Integer.toString(i);
			
			int mok = stNum.length() / 2;
			
			boolean result = false;
			if(i<10) {
				mok = 1;
			}
			
			for(int j = 0; j< mok; j++) {
				int firstNum = stNum.charAt(j);
				int lastNum = stNum.charAt(stNum.length() -1-j);
				if(firstNum == lastNum) {
					result = true;
				}else {
					result = false;
					break;
				}
			}
			if(result == true) {
				list.add(i);
			}
			
		}
		return list;
	}
	
	
}