백준 27433 팩토리얼
n! 재귀함수
package Baekjoon;
import java.util.Scanner;
public class B27433 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
Long n = sc.nextLong();
System.out.println(factorial(n));
}
public static Long factorial(Long n) {
if (n == 0 || n == 1)
return 1L;
else
return n * factorial(n - 1);
}
}
백준 10870 피보나치
0과 1에 주의할 것
package Baekjoon;
import java.util.Scanner;
public class B10870 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
Long n = sc.nextLong();
System.out.println(fibonacci(n+1));
}
public static Long fibonacci(Long n) {
if(n==1)
return 0L;
else if (n==2)
return 1L;
else {
return fibonacci(n-2)+fibonacci(n-1);
}
}
}
백준 2447 별찍기
package Baekjoon;
import java.util.Scanner;
public class B2447 {
static char[][] arr;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
arr = new char[N][N];
star(0, 0, N, false);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
sb.append(arr[i][j]);
}
sb.append('\n');
}
System.out.print(sb);
}
static void star(int x, int y, int N, boolean blank) {
if (blank) {// 공백
for (int i = x; i < x + N; i++) {
for (int j = y; j < y + N; j++) {
arr[i][j] = ' ';
}
}
return;
}
if (N == 1) {
arr[x][y] = '*';
return;
}
int size = N / 3;
int count = 0;
for (int i = x; i < x + N; i += size) {
for (int j = y; j < y + N; j += size) {
count++;
if (count == 5) { // blank 별이 4개 찍히고 1칸 공백
star(i, j, size, true);
} else {
star(i, j, size, false);
}
}
}
}
}