매일 Algorithm

신재원·2023년 5월 1일
0

Algorithm

목록 보기
111/243

백준 15792번 (Bronze 1)

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;

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

        // 큰 실수를 처리하기 위한 BigDecimal
        BigDecimal number1 = in.nextBigDecimal();
        BigDecimal number2 = in.nextBigDecimal();

        // 1000은 소수점 아래 자릿수, 
        // RoundingMode.DOWN은 버림 실행 : 1000의 자릿수 아래 소수점들은 버린다
        System.out.println(number1.divide(number2, 1000, 
                RoundingMode.DOWN));

    }
}

백준 17202번 (Bronze 1)

import java.util.Scanner;

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

        String number = in.next();
        String check = in.next();

        String total = "";

        for (int i = 0; i < 8; i++) {
            // total 변수에 number, check 문자를 하나씩 번갈아 가며 담는다.
            total += number.charAt(i);
            total += check.charAt(i);
        }

        // total의 길이가 2가 될때까지
        while (total.length() > 2) {
            String result = "";
            for (int i = 0; i < total.length() - 1; i++) {
                int sum = total.charAt(i) - '0' +
                        total.charAt(i + 1) - '0';
                // 두자리수 인경우 일의 자리수만 반환
                result += sum % 10;
            }
            total = result;
        }

        System.out.println(total);
    }
}

프로그래머스 (주사위 게임 2)

public class problem364 {

    class Solution {
        public int solution(int a, int b, int c) {
            int answer = 0;


            // 세개의 숫자가 다른경우
            if (a != b && a != c && b != c) {
                answer = a + b + c;
            }
            // 세개의 숫자가 모두 같은경우
            else if (a == b && a == c && b == c) {
                answer = (a + b + c) *
                        ((a * a) + (b * b) + (c * c)) *
                        ((a * a * a) + (b * b * b) + (c * c * c));
            }
            // 세개의 숫자중 어느 두숫자는 같고, 나머지 숫자는 다른경우
            else {
                answer = (a + b + c) *
                        ((a * a) + (b * b) + (c * c));
            }
            return answer;
        }
    }
}

프로그래머스 (수 이어붙이기)

class Solution {
    public int solution(int[] num_list) {
        int answer = 0;
        String total1 = ""; // 짝수 누적
        String total2 = ""; // 홀수 누적
        for(int i = 0; i < num_list.length; i++){
            if(num_list[i] % 2 == 0){
                total1 += num_list[i];
            }else{
                total2 += num_list[i];
            }
        }
        answer = Integer.parseInt(total1)+ Integer.parseInt(total2);
        return answer;
    }
}

0개의 댓글