23년 4월 21일 [알고리즘 - 수학]

sua·2023년 4월 21일
0

알고리즘 가보자고

목록 보기
5/101

백준 1212번 8진수 2진수

문제

링크 : https://www.acmicpc.net/problem/1212

나의 풀이

import java.util.*;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String[] eight = {"000","001","010","011","100","101","110","111"};
        String str = sc.next();
        boolean start = true;
        
        if (str.length() == 1 && str.charAt(0) == '0') {
            System.out.print(0);
        }
        
        for(int i = 0; i < str.length(); i++) {
            int n = str.charAt(i) - '0';
            if(start == true && n < 4) {
                if(n == 0) {
                    continue;
                } else if(n == 1) {
                    System.out.print("1");
                } else if(n == 2) {
                    System.out.print("10");
                } else if(n == 3) {
                    System.out.print("11");
                }
                start = false;
            } else {
                System.out.print(eight[n]);
                start = false;
            }
        }
    }
}

2진수 8진수 문제를 응용해서 풀면 된다.

결과


백준 2089번 -2진수

문제

링크 : https://www.acmicpc.net/problem/2089

나의 풀이

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        int n = sc.nextInt();

        if(n == 0) {
            System.out.println(0);
        } else {
            while(n != 1) {
                sb.append(Math.abs(n % -2));
                n = (int) Math.ceil((double) n / -2);
            }
            sb.append(n);
        }
        System.out.println(sb.reverse());
    }
}

결과


백준 17103번 골드바흐 파티션

문제

링크 : https://www.acmicpc.net/problem/17103

나의 풀이

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        boolean check[] = new boolean[1000001];
        check[0] = check[1] = true;
        
        for(int i = 0; i * i <= 1000000; i++) {
            if(check[i]) {
                continue;
            }
            for(int j = i * 2; j <= 1000000; j += i) {
                check[j] = true;
            }
        }

        for(int i = 0; i < t; i++) {
            int n = sc.nextInt();
            int count = 0;
            for(int j = 2; j <= n / 2; j++) {
                if(!check[j] && !check[n - j]) {
                    count++;
                }
            }
            System.out.println(count);
        }
    }
}

골드바흐의 추측 문제 풀이에서 응용하여 쌍들을 출력하는 것이 아니라 개수를 구해주면 된다.

결과


백준 11653번 소인수분해

문제

링크 : https://www.acmicpc.net/problem/11653

나의 풀이

import java.util.*;

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

        for(int i = 2; i * i <= n; i++) {
            while(n % i == 0) {
                System.out.println(i);
                n /= i;
            }
        }

        if(n > 1) {
            System.out.println(n);
        }
    }
}

n을 소인수분해 했을 때 나타낼 수 있는 인수 중에서 가장 큰 값을 루트 n이다. 따라서, 2부터 루트 n까지 for문을 돌면서 n을 나눌 수 있으면 나눌 수 없을 때 까지 계속해서 나누고 그 나눈 인수를 계속 출력시켜주게 구현하면 된다.

결과


백준 11005번 진법 변환 2

문제

링크 : https://www.acmicpc.net/problem/11005

나의 풀이

import java.util.*;

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

        StringBuilder sb = new StringBuilder();
        while(n > 0) {
            int r = n % b;
            if(r < 10) {
                sb.append((char) (r + '0'));
            } else {
                sb.append((char) (r - 10 + 'A'));
            }
            n /= b;
        }

        System.out.println(sb.reverse());
    }
}

나머지가 10보다 클때는 알파벳 대문자로 표기할 수 있도록 구현하였다.

결과


백준 2745번 진법 변환

문제

링크 : https://www.acmicpc.net/problem/2745

나의 풀이

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        int b = sc.nextInt();

        int answer = 0;
        for(int i = 0; i < n.length(); i++) {
            char c = n.charAt(i);
            if('0' <= c && c <= '9') {
                answer = answer * b + (c - '0');
            } else {
                answer = answer * b + (c - 'A' + 10);
            }
        }
        System.out.println(answer);
    }
}

진법 변환 2 문제에서 풀었던 풀이를 역으로 풀면 된다.

결과


백준 11576번 Base Conversion

문제

링크 : https://www.acmicpc.net/problem/11576

나의 풀이

import java.util.*;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int n = sc.nextInt();
        int answer = 0;
        for(int i = 0; i < n; i++) {
            int x = sc.nextInt();
            answer = answer * a + x;
        }
        convert(answer, b);
    }
    public static void convert(int num, int base) {
        if(num == 0) {
            return;
        }
        convert(num / base, base);
        System.out.print(num % base + " ");
    }
}

결과

profile
가보자고

0개의 댓글