4)
    public static int getMax(int a, int b) {
        int result;
        result = Math.max(a, b);

        return result;
    }




5)
    public static boolean isPrime(int num) {
        boolean result;
        result = false;
        int count = 0;

        for (int i = 1; i <= num; i++) {
            if( num % i == 0 ) {
                count++;
            }
        }
        if (count == 2) {
            result = true;
        }

        return result;
    }




6)
    public static String[] filterStrings(String... input) {
        String[] output;
        int count = 0;              // 배열의 길이를 알기 위해
        for (String s : input) {
            if (s == null){
                continue;
            }
            if (s.trim().length() >= 3 && s.trim().length() <= 4){
                count++;
            }
        }
        output = new String[count]; // output 길이 설정
        count = 0;                  // 인덱스용
        for (String s : input) {
            if (s == null){
                continue;
            }
            if (s.trim().length() >= 3 && s.trim().length() <= 4){
                output[count] = s.trim();
                count++;
            }
        }

        return output;
    }



7)
    public static double getMedian(int... input) {
        double output;
        int max = 0;
        int min = 0;
        boolean minOrig = true;
        for (int num : input) {
            if (minOrig) {
                minOrig = false;
                min = num;
            }
            if (max < num) {
                max = num;
            }
            if (min > num) {
                min = num;
            }
        }
        output = (double) (min + max) / 2;
        return output;
    }



8)
    public static boolean isStraight(char[] input) {
        boolean result;
        result = true;          // 일단 true
        int prevNum = 0;        // 이전 번호
        boolean first = false;  // 첫번째 비교냐 아니냐의 여부

        for (char c : input) {  // for문으로 문자들을 돌림
            int currentNum = c; // 문자를 숫자화해서 현재 번호에 저장
            if ( currentNum - 1 != prevNum && first) {  // 이전 번호가 현재 번호에서 -1 한 것과 다른가?
                result = false;                     // 다를 경우 false
            }
            first = true;           // 처음 for문 돌릴땐 위의 if문은 그냥 통과
            prevNum = currentNum;   // 이전 번호에 현재 번호 저장
        }

        return result;
    }

 

'코딩 문제(JavaScript)' 카테고리의 다른 글

240801~16 푼 문제  (0) 2024.08.17
240729~31 푼 문제  (0) 2024.07.31
240716~18 푼 문제  (0) 2024.07.16
240708~09 푼 문제  (0) 2024.07.09
240423~30 푼 문제  (0) 2024.04.23

+ Recent posts