본문 바로가기
Problem Solving/Baekjoon

[백준] 13410 거꾸로 구구단 - Brute Force / Java

by graycode 2023. 12. 27.

 문제 링크

 

13410번: 거꾸로 구구단

일반적인 구구단에서 가장 큰 수는 마지막 항의 값이 제일 크다. 거꾸로 구구단에서는, 각 항에 구구단의 계산 결과로 나온 값을 뒤집어 저장을 한다. 이렇게 하면 가장 큰 값이 항상 마지막이

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = read(), k = read(), max = n;
        while (k > 2) max = Math.max(max, reverse(n * k--));

        bw.write(String.valueOf(max));
        bw.flush();
    }

    private static int reverse(int src) {
        int tgt = 0;
        do tgt = tgt * 10 + src % 10; while ((src /= 10) > 0);

        return tgt;
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

 

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = read(), k = read(), max = n;
        while (k > 2)
            max = Math.max(max, Integer.parseInt(new StringBuilder((String.valueOf(n * k--))).reverse().toString()));

        bw.write(String.valueOf(max));
        bw.flush();
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글