본문 바로가기

프로그래머스/lv0

제곱수 판별

/* 제곱수 판별
 * 어떤 자연수를 제곱했을 때 나오는 정수를 제곱수라고 합니다.
 * 정수 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*== 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

'프로그래머스 > lv0' 카테고리의 다른 글

문자열 정렬하기(2)  (0) 2022.12.06
세균 증식  (0) 2022.12.06
문자열안에 문자열  (0) 2022.12.06
OX 퀴즈  (0) 2022.12.06
자릿수 더하기  (0) 2022.12.06