๋ฌธ์ œ ์„ค๋ช…


๐Ÿ’ก Info

๋‚ด์šฉ

ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋Š” 0๊ณผ 1๋กœ ์‹œ์ž‘ํ•œ๋‹ค. 0๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋Š” 0์ด๊ณ , 1๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋Š” 1์ด๋‹ค. ๊ทธ ๋‹ค์Œ 2๋ฒˆ์งธ ๋ถ€ํ„ฐ๋Š” ๋ฐ”๋กœ ์•ž ๋‘ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์˜ ํ•ฉ์ด ๋œ๋‹ค.

์ด๋ฅผ ์‹์œผ๋กœ ์จ๋ณด๋ฉด Fnย = Fn-1ย + Fn-2ย (n โ‰ฅ 2)๊ฐ€ ๋œ๋‹ค.

n=17์ผ๋•Œ ๊นŒ์ง€ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋ฅผ ์จ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597

n์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, n๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๐Ÿ“ฅ์ž…๋ ฅ ์กฐ๊ฑด

  • ์ฒซ์งธ ์ค„์— n์ด ์ฃผ์–ด์ง„๋‹ค. n์€ 20๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜ ๋˜๋Š” 0์ด๋‹ค.
    10

๐Ÿ“ค์ถœ๋ ฅ ์กฐ๊ฑด

  • ์ฒซ์งธ ์ค„์— n๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
    55


์œ ์˜ ์‚ฌํ•ญ ๋ฐ ์ œ์•ฝ ์กฐ๊ฑด


.



๋ฌธ์ œ ์ดํ•ด


  • n์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, n๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑํ•˜๊ธฐ


์•Œ๊ณ ๋ฆฌ์ฆ˜


์‹ค์ œ ํ’€์ด ์‹œ๊ฐ„ : 12๋ถ„

  • ์ž…๋ ฅ
    • n ์ž…๋ ฅ๋ฐ›๊ธฐ
  • ๊ณ„์‚ฐ
    • f(0) = 0, f(1) = 1 ์ง€์ •ํ•˜๊ธฐ
    • ๊ทธ ํ›„์˜ i=2; i<n; i++๋กœ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด ๊ตฌํ•˜๊ธฐ
      • f(n) = f(n-1) + f(n-2)
  • ์ถœ๋ ฅ
    • n๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์ธ f[n] ์ถœ๋ ฅํ•˜๊ธฐ
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		int[] f = new int[n+1];
		
		if(n <=1) {
			f[n] = n;
		}
		
		for(int i=2; i<n; i++) {
			f[i] = f[i-1] + f[i-2];
		}
		System.out.println(f[n]);
	}

}


์˜ค๋‹ต ์ฒดํฌ


  • 10์„ ์ž…๋ ฅํ•˜๋ฉด ์ •์ƒ์ ์ธ ์ˆ˜๊ฐ€ ์•„๋‹Œ 0์ด ๋‚˜์˜ค๋Š” ํ˜„์ƒ ๋ฐœ์ƒ
    • if๋ฌธ ์„ ์–ธ ์ „์— f(0), f(1)์˜ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ธฐ
    • ํ•˜๋‚˜์˜ if๋ฌธ ์•ˆ์— 2 ์ด์ƒ์˜ ๊ฐ’ ๊ณ„์‚ฐ์‹์„ ๋„ฃ์–ด์„œ ํ•ด๊ฒฐ
  • ๋˜ํ•œ, ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ ๋ฐœ์ƒ
    • n ๋ฐฐ์—ด์˜ ๋ฒ”์œ„๋ฅผ n+2๋กœ ์ง€์ •ํ•ด์•ผํ•จ!!

      //before
      if(n <= 1) {
      	f[n] = n;
      }
      
      for(int i=2; i<n; i++) {
      	f[i] = f[i-1] + f[i-2];
      }
      System.out.println(f[n]);
      }
      //after
      f[0] = 0;
      f[1] = 1;
      
      if (n > 1) {
          for (int i = 2; i <= n; i++) {
              f[i] = f[i - 1] + f[i - 2];
          }
      }
      System.out.println(f[n]);
      


์ตœ์ข… ํ’€์ด


์‹ค์ œ ํ’€์ด ์‹œ๊ฐ„ : 28๋ถ„(์ฒซ ํ’€์ด ํฌํ•จ)

  • ์ž…๋ ฅ
    • n ์ž…๋ ฅ๋ฐ›๊ธฐ
  • ๊ณ„์‚ฐ ( n > 1)
    • f(0) = 0, f(1) = 1 ์ง€์ •ํ•˜๊ธฐ
    • ๊ทธ ํ›„์˜ i=2; i<n; i++๋กœ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด ๊ตฌํ•˜๊ธฐ
      • f(n) = f(n-1) + f(n-2)
  • ์ถœ๋ ฅ
    • n๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์ธ f[n] ์ถœ๋ ฅํ•˜๊ธฐ
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		int[] f = new int[n+2];
		
		f[0] = 0;
		f[1] = 1;
        
		if (n > 1) {
            for (int i = 2; i <= n; i++) {
                f[i] = f[i - 1] + f[i - 2];
            }
        }
        System.out.println(f[n]);
    }
}

profile
์–ธ์  ๊ฐ€ ๋‚ด ์ฝ”๋“œ๋กœ ์„ธ์ƒ์— ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ๋„๋ก, BE&Data Science ๊ฐœ๋ฐœ ๊ธฐ๋ก ๋…ธํŠธโ˜˜๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€