매일 Algorithm

신재원·2023년 2월 17일
0

Algorithm

목록 보기
40/243

백준 10870번 (재귀)

import java.util.Scanner;

public class problem91 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int a = in.nextInt();

        // 입력값의 최대는 20이하 이다.
        int [] arr = new int[21];
        arr[0] = 0;
        arr[1] = 1;
        arr[2] = 1;

        // arr[2] = 1 arr[3] = 1 + 1 arr[4] = 2 + 1 arr[5] = 3 + 2

        for(int i = 2; i < arr.length; i++) {

            arr[i] = arr[i-1] + arr[i-2];
        }

        System.out.println(arr[a]);

    }
}

백준 10872번 (재귀)

import java.util.Scanner;

public class problem92 {
    public static void main(String[] args) {
        // 간단한 재귀 문제이다. for문 사용
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();

        int total = 1;

        if(a == 0) {
            System.out.println(total);
        }else {
            for (int i = a; i > 0; i--) {
                total *= i;
            }
            System.out.print(total);
        }

    }
}

/*
public class problem92 {
    public static void main(String[] args) {
        // 간단한 재귀 문제이다. 재귀문 사용
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();

        System.out.println(Factorial(a));

    }

    private static int Factorial(int a) {

        if(a <= 1){
            return 1;
        }return a * Factorial(a -1);
    }
}
*/

백준 2231번 (브루트 포스)

import java.util.Scanner;

public class problem93 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int n = in.nextInt();


        int result = 0;
        // 216 = 198, 207 두가지 경우가 존재하는데 198이 최소임으로 198 반환
        for(int i = 0; i < n; i++) {
            int number = i;
            int total = 0; // 자릿수의 합을 담을 변수 초기화

            while(number != 0) {
                total += number % 10;
                number /= 10;
            }

            // 예시에서 기존입력된 값 + 자릿수의 합이다.
            // total(자릿수 누적합) = 9  + 198
            if(total + i == n) {
                result = i;
                break;
            }
        }
        System.out.print(result);
    }
}

백준 7568번 (브루트 포스)

import java.util.Scanner;

public class problem94 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int size = in.nextInt();

        int [][] arr = new int[size][2];

        for(int i = 0; i< size; i++){
            arr[i][0] = in.nextInt(); // 몸무게
            arr[i][1] = in.nextInt(); // 키
        }


        for(int i = 0; i <size; i++){
            int rank = 1; // 순위는 1부터 시작, 초기화

            for(int j = 0; j < size; j++){
                if (i == j) continue; // 같은사람은 비교 X

                if(arr[i][0] < arr[j][0] && arr[i][1] < arr[j][1]){
                    rank++;
                }
            }
            System.out.print(rank + " ");

        }

    }
}

백준 1436번 (브루트포스)

import java.util.Scanner;

public class problem95 {
    public static void main(String[] args) {

        // 입력
        Scanner in = new Scanner(System.in);

        int input = in.nextInt();

        int six = 666;
        int count = 1;

        // count = 1
        while(input != count){
            // 입력값 (n) : 2, six = 1665,
            six++;

            if(String.valueOf(six).contains("666")){
                count++;
                // count = 2, 루프문 탈출
            }
        }

        System.out.print(six);

    }
}

0개의 댓글