매일 Algorithm

신재원·2023년 2월 16일
0

Algorithm

목록 보기
39/243

백준 11508 (그리디 알고리즘)

import java.util.Arrays;
import java.util.Scanner;
public class problem86 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int size = in.nextInt();

        int [] arr = new int[size];

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

        Arrays.sort(arr);

        int index = 0;
        int [] temp = new int[arr.length];
        // 내림차순으로 정렬
        for(int i = arr.length - 1; i >= 0; i--) {
            temp[index] = arr[i];
            index++;
        }
        
        // 6 5 5 5 5 4
        int count = 0;
        for(int i = 0; i <arr.length; i++) {
         // temp배열의 i index값이 3번째값만 건너뛰면된다. (내림차순을 해줬기때문)
            if(i % 3 == 2) {
                continue;
            }
            count += temp[i];
        }

        System.out.print(count);
    }
}

프로그래머스 dev매칭 웹 백엔드 문제

import java.util.Arrays;

public class problem87 {
    import java.util.Arrays;
    class Solution {
        public int[] solution(int[] lottos, int[] win_nums) {
            
            Arrays.sort(lottos);
            Arrays.sort(win_nums);
            int count = 0;
            for(int i = 0; i < lottos.length; i++){
                if(lottos[i] == 0){
                    count++;
                }
            }

            int match = 0;
            int total = 0;
            // win_nums의 배열을 다 훑어서 갯수를 세어준다.
            for(int i = 0; i <lottos.length; i++){
                for(int j = 0; j < win_nums.length; j++){
                    if(lottos[i] == win_nums[j]){
                        match++;
                        total++;
                    }
                }
            }
            total = total + count;
            if(match == 6){
                match = 1;
            }else if(match == 5){
                match = 2;
            }else if(match == 4){
                match =3;
            }else if(match == 3){
                match = 4;
            }else if (match == 2){
                match = 5;
            }else{
                match = 6;
            }

            if(total == 6){
                total = 1;
            }else if(total == 5){
                total = 2;
            }else if(total == 4){
                total =3;
            }else if(total == 3){
                total = 4;
            }else if (total == 2){
                total = 5;
            }else{
                total = 6;
            }
            int[] answer = {total, match};
            return answer;
        }
    }
}

백준 2490번 (구현)

import java.util.Scanner;


public class problem88 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int [] size = new int [4];
        for(int i = 0; i < 3; i++) {
            int count = 0 ;
            for(int j = 0; j < size.length; j++) {
                size[j] = in.nextInt();
                if(size[j] == 1){
                    count++;
                }
            }
            // 배 0 등 1
            if(count == 4) {
                System.out.println('E');
            }else if(count == 3) {
                System.out.println('A');
            }else if(count == 2) {
                System.out.println('B');
            }
            else if(count == 1) {
                System.out.println('C');
            }else {
                System.out.println('D');
            }

        }

    }
}

백준 9095번 (다이나믹 프로그래밍)

import java.util.Scanner;

public class problem89 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int size = in.nextInt();

        int [] arr = new int [11];
        // 초기값
        // 1, 2, 3으로 값을 만드는 것임으로
        arr[1] = 1; // 1
        arr[2] = 2; // 1+1 , 2
        arr[3] = 4; // 1+1+1, 1+2, 2+1

        for(int i = 0; i < size; i++) {
            int temp = in.nextInt();

            // 점화식
            for (int j = 4; j <= temp; j++) {
                arr[j] = arr[j - 3] + arr[j - 2] + arr[j - 1];
            }
            System.out.println(arr[temp]);

        }
    }
}

백준 1476번 (브루트포스)

import java.util.Scanner;

public class problem90 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);


        int e = in.nextInt();
        int s = in.nextInt();
        int m = in.nextInt();

        int E = 0;
        int S = 0;
        int M = 0;

        int year = 0;

        while (true) {
            E++;
            S++;
            M++;
            year++;
            if (E == 16) E = 1;
            if (S == 29) S = 1;
            if (M == 20) M = 1;

            // 입력된 값이 whlie 루프문에 값이랑 같을때 까지 반복
            if (e == E && m == M && s == S) break;
        }
        System.out.print(year);
    }

}

0개의 댓글