Bytelandian gold coins

//http://www.spoj.com/problems/COINS/
#include <iostream>
#include <cstdlib>
using namespace std;

#define MEMOSIZE 1000000
unsigned memo[MEMOSIZE];
unsigned maxprofit(unsigned n) {
	unsigned s;
	if ((n<MEMOSIZE)&&memo[n]) return memo[n]-1;
	s=maxprofit(n/2)+maxprofit(n/3)+maxprofit(n/4);
	s=(s>n)?s:n;
	if(n<MEMOSIZE) memo[n]=s+1;
	return s;
}
int main(int argc, char* argv[]) {
	unsigned n;
	for(n=0; n<12; n++) memo[n]=n+1;
		while(scanf("%u", &n)==1) cout << maxprofit(n) << endl;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章