/* 제곱수 판별
* 어떤 자연수를 제곱했을 때 나오는 정수를 제곱수라고 합니다.
* 정수 n이 매개변수로 주어질 때,
* n이 제곱수라면 1을 아니라면 2를 return하도록 solution 함수를 완성해주세요.
*
* n result
* 144 1 144는 12의 제곱이므로 제곱수입니다. 따라서 1을 return
* 976 2 976은 제곱수가 아닙니다. 따라서 2를 return
*/
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 | public class programmer_0_10 { static int a1 = 144; static int a2 = 976; public int solution(int n){ int answer = 0; for(int i = 1; i < n; i++){ if(i*i == n){ answer = 1; break; } else answer = 2; } return answer; } public static void main(String args[]){ programmer_0_10 t = new programmer_0_10(); System.out.println("---------------------------------------"); System.out.println("result = " + t.solution(a1)); System.out.println("---------------------------------------"); System.out.println("result2 = " + t.solution(a2)); System.out.println("---------------------------------------"); } } | cs |