๐ก 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๋ฒ์งธ ํผ๋ณด๋์น ์๋ฅผ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
๐ฅ์ ๋ ฅ ์กฐ๊ฑด
10
๐ค์ถ๋ ฅ ์กฐ๊ฑด
55
.
์ค์ ํ์ด ์๊ฐ : 12๋ถ
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]);
}
}
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๋ถ(์ฒซ ํ์ด ํฌํจ)
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]);
}
}