매일 Algorithm

신재원·2023년 5월 3일
0

Algorithm

목록 보기
112/243

프로그래머스 (배열 만들기 2)

public class problem367 {
    public static void main(String[] args) {
        class Solution {
            public boolean isZeroOrFive(int number) {
                // 숫자 0과 5로만 이루어져 있는지 검증
                while (number > 0) {
                    int remainder = number % 10;
                    int temp = remainder % 5;
                    
                    // 0과 5 이외의 숫자가 있다면 false 반환
                    if (temp != 0) { 
                        return false;
                    }
                    number /= 10;
                }
                return true;
            }

            public int[] solution(int l, int r) {
                int count = 0; // 숫자 0과 5로만 이루어진 정수의 개수 count
                for (int i = l; i <= r; i++) {
                    if (isZeroOrFive(i)) {
                        count++; // count 증가
                    }
                }
                // 0과 5로만 이루어진 정수가 없다면
                if (count == 0) {
                    return new int[]{-1};
                }
                // 숫자 0과 5로만 이루어진 정수를 담을 배열 생성
                int[] answer = new int[count];
                int index = 0;
                for (int i = l; i <= r; i++) {
                    if (isZeroOrFive(i)) {
                        answer[index] = i;
                        index++;
                    }
                }
                return answer;
            }
        }
    }
}

프로그래머스 (약수의 합)

public class problem368 {
    class Solution {
        public int solution(int n) {
            int answer = 0;

            for(int i = 1; i <= n; i++){
                if(n % i == 0){
                    answer += i;
                }
            }
            return answer;
        }
    }
}

프로그래머스 (특이한 이차원 배열 1)

public class problem366 {
    class Solution {
        public int[][] solution(int n) {
            int[][] answer = new int[n][n];

            // 0,0 1,1 배열 순으로 1을 할당합니다.
            for (int i = 0; i < n; i++) {
                answer[i][i] = 1;
            }
            return answer;
        }
    }
}

코드업 (1084번)

import java.io.*;

public class CodeUp1084 {
    public static void main(String[] args) throws IOException {

        BufferedReader br =
                new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw =
                new BufferedWriter(new OutputStreamWriter(System.out));

        // 2 2 2를 split 으로 받음
        String[] s = br.readLine().split(" ");

        int count = 0;
        for (int i = 0; i < Integer.parseInt(s[0]); i++) {
            for (int j = 0; j < Integer.parseInt(s[1]); j++) {
                for (int k = 0; k < Integer.parseInt(s[2]); k++) {
                    bw.write(i + " " + j + " " + k + "\n");
                    count++;
                }
            }
        }
        bw.write(String.valueOf(count));
        bw.flush();
    }
}

코드업 (1620번)

import java.util.Scanner;

public class problem369 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int number = in.nextInt();

        int sum = 0;

        while (number > 0) {
            sum += number % 10;
            number /= 10;
        }
        // sum이 두자리 인경우
        while (sum >= 10) {
            int newSum = 0;

            while (sum > 0) {
                newSum += sum % 10;
                sum /= 10;
            }

            sum = newSum;
        }
        System.out.println(sum);
    }
}

0개의 댓글