일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- 코딩테스트 연습
- it 뉴스
- 싸피
- SSAFY
- Java
- SSAFYcial
- IT 트렌드
- IT 동향
- 백준
- 신문 스크랩
- 백준위
- 구글
- 신문스크랩
- it 이슈
- 싸피셜
- SSAFY 7기
- html
- 네트워크 관리사
- python
- KT
- java 객체지향 프로그래밍
- 리얼클래스
- 네트워크 관리사 2급 실기
- 코딩테스트
- 네트워크 관리사 2급
- 코테
- 인앱결제
- 우테코
- 프로그래머스
- 카카오
Archives
- Today
- Total
개발자일걸요..?
1003번 피보나치 함수 본문
728x90
반응형
문제링크 : www.acmicpc.net/problem/1003
1003번: 피보나치 함수
각 테스트 케이스마다 0이 출력되는 횟수와 1이 출력되는 횟수를 공백으로 구분해서 출력한다.
www.acmicpc.net
방법1. 재귀함수 (시간복잡도 : O(2^N)) => 시간 초과
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Main {
static int zero =0;
static int one = 0;
public static int fibonacci(int i) {
if (i==0) {
zero+=1;
return 0;
}
else if(i==1) {
one+=1;
return 1;
}
else return fibonacci(i-1)+fibonacci(i-2);
}
public static void main(String[] args) {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb= new StringBuilder();
try {
int N = Integer.parseInt(bf.readLine());
for (int n = 0;n<N;n++) {
try {
zero=0;
one=0;
int a = Integer.parseInt(bf.readLine());
int answer = fibonacci(a);
sb.append(zero+" "+one+"\n");
} catch(IOException e) {}
}
System.out.println(sb);
} catch(IOException e) {}
}
}
방법2. 동적프로그래밍 (시간복잡도 : O(N^2)) => 시간 : 172ms
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main{
static int arr[] = new int[41];
public static int fibonacci(int n) {
if(n==0) {
arr[n] = 0;
return 0;
}
else if(n==1) {
arr[n] = 1;
return 1;
}
if(arr[n]!=0) {return arr[n];}
else {
return arr[n] = fibonacci(n-1)+fibonacci(n-2);
}
}
public static void main(String[] args) {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
try {
int N = Integer.parseInt(bf.readLine());
for(int i=0;i<N;i++) {
try {
int A = Integer.parseInt(bf.readLine());
if(A==0) {
sb.append("1"+" "+"0"+"\n");
}
else if(A==1) {
sb.append("0"+" "+"1"+"\n");
}
else{
fibonacci(A);
sb.append(arr[A-1]+" "+arr[A]+"\n");
}
}catch(IOException e) {}
}
System.out.println(sb);
}catch(IOException e) {}
}
}
방법3. 반복문 (시간복잡도 : O(N)) =>시간 : 164ms
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb= new StringBuilder();
try {
int N = Integer.parseInt(bf.readLine());
for(int i =0;i<N;i++) {
try {
int target = Integer.parseInt(bf.readLine());
int list[][] = new int[41][2];
list[0][0] = 1;
list[0][1] = 0;
list[1][0] = 0;
list[1][1] = 1;
for(int j = 2;j<=target;j++) {
list[j][0] = list[j-1][0]+list[j-2][0];
list[j][1] = list[j-1][1]+list[j-2][1];
}
sb.append(list[target][0]+" "+ list[target][1]+"\n");
}
catch(IOException e) {}
}
System.out.println(sb);
}
catch(IOException e) {}
}
}
참고1 : galid1.tistory.com/507
알고리즘 - Dynamic Programming(동적프로그래밍)이란?
Dynamic Programming(동적계획법) 이란 1. Dynamic Programming(동적계획법)이란? 큰 문제를 작은문제로 나누어 푸는 문제를 일컫는 말입니다. 동적 계획법이란 말 때문에 어떤 부분에서 동적으로 프로그래
galid1.tistory.com
참고2 : makefortune2.tistory.com/60
28. 피보나치 수열( 재귀, 동적 프로그래밍, 반복) 모든 방식 알고리즘
1. 개요 피보나치 수열의 알고리즘은 정말 쉬움. 크게 3가지 방식 존재. (1) 재귀(recursion), (2) 동적 프로그래밍(Dynamic Programming), (3) 반복(For문) 피보나치 수열의 BigO()는 방식에 따라 다름. ..
makefortune2.tistory.com
반응형
'알고리즘코딩 > Baekjoon Online Judge' 카테고리의 다른 글
5086번 배수와 약수 (0) | 2021.02.11 |
---|---|
14889번 스타트와 링크 (0) | 2021.02.11 |
15652번 N과 M(4) (0) | 2021.02.06 |
15651번 N과 M(3) (0) | 2021.02.06 |
15650번 N과 M(2) (0) | 2021.02.06 |
Comments