๊ธฐ๋ก๋ฐฉ

BOJ_2635 : ์ˆ˜ ์ด์–ด๊ฐ€๊ธฐ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_2635 : ์ˆ˜ ์ด์–ด๊ฐ€๊ธฐ

Soom_1n 2022. 10. 8. 22:25

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

2635๋ฒˆ: ์ˆ˜ ์ด์–ด๊ฐ€๊ธฐ

์ฒซ ๋ฒˆ์งธ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด ์ˆ˜๋Š” 30,000 ๋ณด๋‹ค ๊ฐ™๊ฑฐ๋‚˜ ์ž‘์€ ์–‘์˜ ์ •์ˆ˜์ด๋‹ค.

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ฃผ์–ด์ง„ ์ž์—ฐ์ˆ˜๋ฅผ ์ž์—ฐ์ˆ˜๋กœ ๋นผ๋ฉฐ ๋งŒ๋“  ์ˆ˜์—ด ์ค‘ ๊ฐ€์žฅ ๊ธด ๊ฒฝ์šฐ์˜ ๊ธธ์ด์™€ ๊ทธ ๋ชฉ๋ก์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        ArrayList<Integer> answer = new ArrayList<>();
        int size = 0;
        for (int i = n; i >= n/2 - 1; i--){
            int deff = n - i;
            ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(n,i));
            size = 2;
            while (deff >= 0){
                arr.add(deff);
                deff = arr.get(++size-2) - arr.get(size-1);
            }
            if (arr.size() > answer.size())
                answer = arr;
        }
        size = answer.size();
        System.out.println(size);
        for (int i = 0; i < size-1; i++)
            System.out.print(answer.get(i) + " ");
        System.out.println(answer.get(size-1));
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • i๋ฅผ ์ž…๋ ฅ๋ฐ›์€ n๋ถ€ํ„ฐ n/2 -1 ๊นŒ์ง€ ๋ฐ”๊ฟ”๊ฐ€๋ฉฐ ์ˆ˜์—ด์„ ๋งŒ๋“ค์–ด๋ณด๊ณ  ๊ธธ์ด๋ฅผ ๋น„๊ตํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€๋Š”๋ฐ ๋ช‡ ๊ฐ€์ง€ ๊ณ„์‚ฐ๋ฒ”์œ„๋ฅผ ์ž˜๋ชป ์ƒ๊ฐํ•ด์„œ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค๊ฐ€ ๋งŽ์ด ๋‚˜์™”๋‹ค.
    • while์กฐ๊ฑด์˜ deff๊ฐ€ 0์ด ๋‚˜์˜ค๋ฉด ์•ˆ๋˜๋Š” ์ค„ ์•Œ์•˜์ง€๋งŒ, ์Œ์ˆ˜๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š๋Š”๊ฒŒ ์กฐ๊ฑด์ด์—ˆ๋‹ค.
    • deff๊ฐ€ 0์ด ๋‚˜์™€๋„ ๋˜๋ฏ€๋กœ i๋„ n-1์ด ์•„๋‹ˆ๋ผ n๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์•ผํ•œ๋‹ค. (n = 1์ธ ๊ฒฝ์šฐ ์˜ค๋‹ต)
  • ํ˜น์‹œ ์˜ค๋‹ต์— ์ถœ๋ ฅ๋ฐฉ์‹์ด ์˜ํ–ฅ์žˆ์„๊นŒ๋ด ๋งˆ์ง€๋ง‰ ๊ณต๋ฐฑ์ด ์—†๋„๋ก ์ถœ๋ ฅ๋„ ๋ฐ”๊ฟจ์ง€๋งŒ ์˜ํ–ฅ์€ ์—†๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

728x90