✔ 나의 답 -> 맞음
import java.util.*;
public class P2_4 {
public static int[] solution(int n){
int[] arr = new int[n];
if(n==1) arr[0]=1;
else if(n==2) {
arr[0]=1;
arr[1]=1;
}
else {
arr[0]=1;
arr[1]=1;
for(int i=2;i<n;i++){
arr[i]=arr[i-1]+arr[i-2];
}
}
return arr;
}
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
for(int x : solution(n))
System.out.print(x+" ");
}
}
✔️ 강의 답
import java.util.*;
public class p2_1 {
public static int[] solution(int n){
int[] arr = new int[n];
arr[0]=1;
arr[2]=1;
for(int i=2;i<n;i++){
arr[i]=arr[i-1]+arr[i-2];
}
return arr;
}
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
for(int x : solution(n))
System.out.print(x+" ");
}
}
import java.util.*;
public class p2_1 {
public static void solution(int n){
int a=1, b=1, c;
System.out.print(a+" "+b+" ");
for(int i=2;i<n;i++) {
c=a+b;
System.out.print(c+" ");
a=b;
b=c;
}
}
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
solution(n);
}
}