관리 메뉴

KorSA

BINOMIAL COEFFICIENT 본문

Algorithm/Dynamic Programming

BINOMIAL COEFFICIENT

Praiv. 2019. 1. 17. 05:30
320x100
#include <iostream>
#include <cstring>

using namespace std;

const int MAX_NUM = 1024;

int cache[MAX_NUM][MAX_NUM];

void initCache();
int binomial(int n, int r);

int main()
{
	int caseNum = 0;
	int n = 0, r = 0;

	cin >> caseNum;

	for (int cIter = 0; cIter < caseNum; cIter++)
	{
		n = r = 0;
		initCache();

		cin >> n >> r;

		cout << binomial(n, r) << endl;
	}
}

void initCache()
{
	for (int i = 0; i < MAX_NUM; i++)
	{
		for (int j = 0; j < MAX_NUM; j++)
		{
			cache[i][j] = -1;
		}
	}
}

int binomial(int n, int r)
{
	if (r == 0 && n == r) return 1;
	if (cache[n][r] != -1) return cache[n][r];
	cache[n][r] = binomial(n - 1, r - 1) + binomial(n - 1, r);

	return cache[n][r];
}



이슈가 하나 생겼다.

int cache[][] 와 int binomial() 로 선언했을 때에는 정상적인 값이 나왔는데,

double cache[][] 와 double binomial() 로 선언했을 때에는 이상한 값이 나왔다. (n == 18, r == 9 일 경우만)


아마 x64 아키텍처에서 4바이트 짜리 int와 8바이트 짜리 double 을 사용하면서 무언가가 꼬인 것 같은데 찾다보면 너무 오래 걸릴 것 같아서 일단은 그냥 넘어감.. 근데 왜 이렇게 동작하는 거지 ..?



BinomialCoefficientInput.txt


BinomialCoefficientOutput.txt


728x90
728x90

'Algorithm > Dynamic Programming' 카테고리의 다른 글

POLY  (0) 2019.01.23
ASYMTILING  (0) 2019.01.23
QUANTIZATION  (0) 2019.01.22
PI  (0) 2019.01.22
WILDCARD  (0) 2019.01.19
Comments