매일 Algorithm

신재원·2023년 2월 14일
0

Algorithm

목록 보기
36/243

백준 1439번 (그리디 알고리즘)

import java.util.Scanner;

public class problem75 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        String input = in.nextLine();

        // char 배열은 .equals가 안됨으로 string, split
        String [] a = input.split("");

        int zero =0;
        int one = 0;
        // 0000001 이라는 입력값이 입력되면 반례가 존재
        if(a[0].equals("0")){
            zero++;
        }else{
            one++;
        }

        // 101
        for(int i = 1; i < a.length; i++) {

            // 001100 == 010
            if(!(a[i-1].equals(a[i]))) {
                if(a[i].equals("0")) {
                    zero++;
                }
                else {
                    one++;
                }
            }
        }

        System.out.println(Math.min(zero, one));
    }

}

백준 2864번 (그리디 알고리즘)

import java.util.Scanner;

public class problem76 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        String a = in.next();
        String b = in.next();

        String minA = "";
        String maxA = "";

        String minB = "";
        String maxB = "";


        for(int i = 0; i < a.length(); i++) {

            if(a.charAt(i) == '6') {
                minA = minA + "5";
            }else {
                // 6이 아닐경우 입력받은 숫자 그대로 대입
                minA = minA + a.charAt(i);
            }

            if(a.charAt(i) == '5') {
                maxA = maxA + "6";
            }else {
                // 5가 아닐 경우 입력받은 숫자 그대로 대입
                maxA = maxA + a.charAt(i);
            }

        }

        for(int i = 0; i < b.length(); i++) {

            if(b.charAt(i) == '6') {
                minB = minB + "5";
            }else {
                // 6이 아닐경우 입력받은 숫자 그대로 대입
                minB = minB + b.charAt(i);
            }

            if(b.charAt(i) == '5') {
                maxB = maxB + "6";
            }else {
                // 5가 아닐 경우 입력받은 숫자 그대로 대입
                maxB = maxB + b.charAt(i);
            }

        }


        int totalmax = Integer.parseInt(maxA) + Integer.parseInt(maxB);
        int totalmin = Integer.parseInt(minA) + Integer.parseInt(minB);


        System.out.print(totalmin + " " + totalmax);
    }

}

백준 2720번 (그리디 알고리즘)

import java.util.Scanner;

public class problem77 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int size = in.nextInt();
        // 간단한 그리디 알고리즘 문제이다.
        while(size > 0) {
            int money = in.nextInt();

            int a = money/25;
            money %=25;
            int b = money/10;
            money %=10;
            int c = money/5;
            money %=5;
            int d = money/1;
            money %=1;
            System.out.println(a + " " + b + " " + c + " " + d);
            size--;
        }
    }
}

백준 14720번 (그리디 알고리즘)

import java.util.Scanner;

public class problem79 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int size = in.nextInt();
        
        int next = 0;
        int [] arr = new int[size];
        for(int i = 0; i < size; i++) {
            arr[i] = in.nextInt();


        }

        int count = 0;
        
        // 우유의 순서대로 번호를 지정
        for(int i = 0 ; i < arr.length; i++ ) {
            if(arr[i] == 0 && next == 0) {
                count++;
                next = 1;
            }

            if(arr[i] == 1 && next == 1) {
                count++;
                next = 2;
            }

            if(arr[i] == 2 && next == 2) {
                count++;
                next = 0;
            }
        }
        System.out.print(count);
    }
}

백준 16435번 (그리디 알고리즘)


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

public class problem80 {

    public static void main(String[] args) {

        // 간단한 그리디 알고리즘 문제다.
        Scanner in = new Scanner(System.in);


        int a = in.nextInt();
        int b = in.nextInt();

        int [] arr = new int[a];

        for(int i = 0 ; i< a; i++) {
            arr[i] = in.nextInt();
        }

        Arrays.sort(arr);
        // 입력된 배열과 b(길이)를 비교
        for(int j = 0 ; j < arr.length; j++) {
            if(b >= arr[j]) {
                b += 1;
            }
        }
        System.out.println(b);
    }

}

백준 20115번 (그리디 알고리즘)

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

public class problem81 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);


        int a = in.nextInt();

        // 소수점 보관을 위해 double
        double [] arr = new double [a];

        for (int i = 0; i < a; i++) {
            arr[i] = in.nextDouble();
        }

        Arrays.sort(arr);

        double [] drink = new double [arr.length];
        int k = 0;
        for(int j = arr.length -1 ; j  >= 0; j--) {
            drink[k] = arr[j];
            k++;
        }

        double total = drink[0];
        // 10 + 4.5 + 2 + 1.5 2
        for(int p = 1; p < drink.length; p++) {
            total += drink[p]/2;

        }
        System.out.print(total);
    }
}

백준 2839번 (그리디 알고리즘)

import java.util.Scanner;

public class problem82 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        // 5kg을 최대한 많이 사용하여 최소갯수를 구해야된다.
        int a = in.nextInt();


        if(a == 4 || a==7) {
            System.out.print(-1);

        }
        // ex = 10, 15 일 경우 5kg을 다 사용할수있다.
        else if(a % 5 == 0) {
            System.out.print((a/5));
        }
        // ex = 11, 13 일경우  3kg 1개가 확정이 된다.
        else if(a % 5 == 1 || a % 5 == 3) {
            System.out.print((a/5)+1);
        }
        // ex = 12, 14 일경우는 3kg 2개가 확정이 된다.
        else if(a % 5 == 2 || a % 5 == 4) {
            System.out.print((a/5) +2);
        }

    }
}

0개의 댓글