매일 Algorithm

신재원·2023년 2월 2일
0

Algorithm

목록 보기
25/243

백준 4796번 (bronze 1)

import java.util.Scanner;

public class problem25 {
    public static void main(String[] args) {
        // ex 8일중에 5일이 캠핑이 가능하다고 하면, 20일 휴가중에
        // 8 * 2 = 16, 5 * 2 = 10, 10일 + 4일 임으로 14일을 사용할수있다.
        Scanner in = new Scanner(System.in);
        StringBuilder stringBuilder = new StringBuilder();

        int root = 1;

        while (true) {
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            if (a == 0 && b == 0 && c == 0) break;
            
   // 5일을 사용할수있는 기간에 4일이 남으면 4일을 다 사용할수 있음으로 min함수 사용
            int result = a * (c / b) + Math.min(a, c % b);
            stringBuilder.append("Case " + root + ": " + result + "\n");
            root++;
        }

        System.out.println(stringBuilder);
    }
}

백준 2609번 (bronze1)

import java.util.Scanner;

public class problem26 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        int gcd = gcd(a, b);
        int lcm = lcm(a, b);

        System.out.println(gcd + " " + lcm);

    }

    // 최대 공약수
    public static int gcd(int a, int b) {

        if (b == 0) return a;
        /*
        ex) a = 24 b = 18 --> gcd(18, 6) --> gcd(6,0)
        */
        return gcd(b, a % b);
    }
    // 최소 공배수
    public static int lcm(int a, int b) {

        return a * b / gcd(a, b);
    }
  
}

백준 1110번 (bronze1)

import java.util.Scanner;

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

        int number = in.nextInt();
        int count = 0;
        int tmp = number;
        // 입력 26, 2 + 6 = 8, 6 + 8 = 84

        while(true) {
            int a = tmp / 10; // 2
            int b = tmp % 10; // 6

            tmp = b * 10 + ((a + b) % 10);
            count++;
            if(tmp == number) break;

        }
        System.out.println(count);
    }
}

백준 1546번 (bronze1)

import java.util.Arrays;
import java.util.Scanner;

public class problem28 {
    public static void main(String[] args) {
        // 새로운 값을 배열에 저장후 평균을 구하는 문제
        Scanner in = new Scanner(System.in);
        int number = in.nextInt();
        // 정답과의 상대/절대 오차는 10-2, 즉 0.01 까지 허용한다.
        // 조건때문에 double형
        double [] arr = new double[number];
        for (int i = 0; i < number; i++) {
            arr[i] = in.nextInt();
        }
        Arrays.sort(arr);

        // 오름차순으로 정렬했음으로, Max 값은 arr의 배열 끝이 가장 큰값이다.
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i] / arr[arr.length-1] * 100;
        }

        double total = 0;
        for (int i = 0; i < arr.length; i++) {
            total += arr[i];
        }
        System.out.println(total / arr.length);

    }
}

0개의 댓글