1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅˆ Silver 5


๐Ÿ“Œ ๋ฌธ์ œ

https://www.acmicpc.net/problem/2635


image


๐Ÿ“Œ ํ’€์ด

์ด ๋ฌธ์ œ๋ฅผ ์ฝ๊ณ , ๋‹จ์ˆœํžˆ ์ฒซ๋ฒˆ์งธ ์ˆซ์ž์—์„œ ๋‘๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ๋บ€ ์ฐจ๋ฅผ ๊ณ„์† ๋นผ์„œ ์Œ์ˆ˜๊ฐ€ ๋ ๋•Œ๊นŒ์ง€์˜ ๊ฐœ์ˆ˜์ธ์ค„ ์•Œ๊ณ , ๊ทธ์— ๋”ฐ๋ผ์„œ ๊ตฌํ˜„์„ ํ•˜์˜€๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ๊นŒ ๋ฌด์กฐ๊ฑด 1๋ณด๋‹ค ์ž‘์€ ์ˆซ์ž์ผ ์ˆ˜ ๋ฐ–์— ์—†๋Š” ์ƒํ™ฉ์ด ๋‚˜์™”๋‹ค.
๋‹ค์‹œ ์ฒœ์ฒœํžˆ ์ฝ์–ด๋ณด๋‹ˆ, ๊ทธ ๋‹ค์Œ ์ˆซ์ž๋“ค๋„ ์ด์ „์ˆซ์ž์™€ ํ˜„์žฌ ์ˆซ์ž๋ฅผ ๋นผ์„œ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.
๊ทธ๋ž˜์„œ ๋งŒ๋“ค๊ณ  ์žˆ๋˜ ์ฝ”๋“œ๋ฅผ ๊ฐˆ์•„์—Ž๊ณ  ์ƒˆ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค.


NumCotinueํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ firstNum๊ณผ secondNum์„ ์ด์šฉํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ๊ฐ€์žฅ ํฐ ๊ฐœ์ˆ˜๋ฅผ ๋งŒ๋“ ๋Š” secondNum์„ ์ด์šฉํ•˜์—ฌ ๋‹ค์‹œ ์ถœ๋ ฅ์„ ํ–ˆ๋‹ค.

๋‹ค์Œ์€ ๊ทธ ์ฝ”๋“œ์ด๋‹ค.


๐Ÿ“Œ Code

package Math;

import java.io.*;
import java.util.StringTokenizer;

public class N2635 {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter((System.out)));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int num = Integer.parseInt(st.nextToken());

        int result = 0;
        int max = 0;
        for (int i = 0; i < num + 1; i++) {
            if ( result < NumContinue(num, i)){
                result  = NumContinue(num, i);
                max = i;
            }
        }
        System.out.println(result);
        Show(num, max);
    }

    public static int NumContinue(int firstNum, int secondNum) {
        int count = 1;
        int thirdNum;
        do {
            thirdNum = firstNum - secondNum;
            firstNum = secondNum;
            secondNum = thirdNum;
            count += 1;
        } while (thirdNum >= 0);
        return count;
    }

    public static void Show(int firstNum, int secondNum) {
        System.out.print(firstNum);
        int thirdNum;
        do {
            thirdNum = firstNum - secondNum;
            firstNum = secondNum;
            secondNum = thirdNum;
            System.out.print(" " + firstNum);
        } while (thirdNum >= 0);

    }

}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.


๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜