[2193] 이친수

Benjamin·2023년 4월 28일
0

BAEKJOON

목록 보기
58/70

💁‍♀️ DP 사용

Troubleshooting

import java.io.IOException;
import java.util.Scanner;

public class makeOne {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		int zeroCnt = 0; 
		int oneCnt = 1;//가장 앞은 1로 시작
		int temp = 0;
		int N = sc.nextInt();
		int[] cnt = new int[N+1]; 
		cnt[1] = 1;
		for(int i=2; i<=N; i++) {
			cnt[i] = zeroCnt * 2 + oneCnt * 1;
			temp = oneCnt;
			oneCnt = zeroCnt;
			zeroCnt = temp + zeroCnt;
		}
		System.out.println(cnt[N]);
	}
}

문제

틀렸습니다를 받았다.

원인

타입의 범위문제였다....

해결

int -> long으로 변경했다.

어이없어할게아니다! 아주 중요한 이슈다. 결국 이것도 실력이다.

제출코드

import java.io.IOException;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		
		// 현재 자리 수 이전 자리수의 경우의 수에서 끝자리 수의 0,1 개수
		long zeroCnt = 0; 
		long oneCnt = 1;//가장 앞은 1로 시작
		
		long temp = 0;
		int N = sc.nextInt();
		long[] cnt = new long[N+1]; 
		cnt[1] = 1;
		for(int i=2; i<=N; i++) {
			cnt[i] = zeroCnt * 2 + oneCnt * 1;
			temp = oneCnt;
			oneCnt = zeroCnt; // 1다음은 0만 올 수있음
			zeroCnt = temp + zeroCnt; //0다음은 0,1둘 다 올 수 있음
		}
		System.out.println(cnt[N]);
	}
}

0개의 댓글