-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
107a58b
commit 7498c01
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package test.boj.dp; | ||
|
||
import org.junit.*; | ||
|
||
import java.util.Scanner; | ||
|
||
public class 타일링_2곱하기n { | ||
|
||
@Test | ||
public void test() { | ||
System.out.println(solution(1000)); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println(solution(sc.nextInt())); | ||
} | ||
|
||
// n: 0 1 2 3 4 5 6 7 8 9 .... | ||
// F(n): 1 1 2 3 5 8 13 21 34 55 ..... | ||
private static int solution(int n) { | ||
return f(n); | ||
} | ||
|
||
private static int f(int n) { | ||
int[] fibo = new int[n + 1]; | ||
fibo[0] = fibo[1] = 1; | ||
|
||
for (int i = 2; i <= n; i++) { | ||
// !!! overflow -> % 10007 | ||
fibo[i] = (fibo[i - 1] + fibo[i - 2]) % 10007; | ||
} | ||
return fibo[n]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package test.boj.dp; | ||
|
||
import org.junit.*; | ||
|
||
import java.util.Scanner; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
// refer images/타일링_2곱하기n_2.png | ||
public class 타일링_2곱하기n_2 { | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println(solution(sc.nextInt())); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
System.out.println(solution(1)); | ||
assertThat(solution(2)) | ||
.isEqualTo(3); | ||
|
||
assertThat(solution(3)) | ||
.isEqualTo(5); | ||
|
||
assertThat(solution(4)) | ||
.isEqualTo(11); | ||
|
||
assertThat(solution(1000)) | ||
.isEqualTo(7326); | ||
} | ||
|
||
private static int solution(int n) { | ||
int[] dp = new int[n + 2]; | ||
dp[1] = 1; | ||
dp[2] = 3; | ||
|
||
for (int i = 3; i <= n; i++) { | ||
dp[i] = (dp[i - 1] + (dp[i - 2] * 2)) % 10_007; | ||
} | ||
return dp[n]; | ||
} | ||
} |
This comment has been minimized.
Sorry, something went wrong.
CheolHoJungMar 26, 2019
Author Owner