피보나치 수열 소스코드: Difference between revisions

From CS Wiki
(새 문서: == C언어 == <syntaxhighlight lang="C" line='line'> #include <stdio.h> int fib(int n) { if(n<=1) return n; else return fib(n-1)+fib(n-2); } int main() { printf("%d", fib(3...)
 
 
(One intermediate revision by one other user not shown)
Line 2: Line 2:
<syntaxhighlight lang="C" line='line'>
<syntaxhighlight lang="C" line='line'>
#include <stdio.h>
#include <stdio.h>
int dp[100];
int fib(int n) {  
int fib(int n) {  
  if(dp[n]) return dp[n];
   if(n<=1) return n;  
   if(n<=1) return n;  
   else return fib(n-1)+fib(n-2);
   else return dp[n] = fib(n-1)+fib(n-2);
}  
}  


Line 11: Line 14:
}
}
</syntaxhighlight>
</syntaxhighlight>
[[분류:알고리즘]]

Latest revision as of 11:40, 10 July 2019

C언어[edit | edit source]

#include <stdio.h>
int dp[100];

int fib(int n) { 
  if(dp[n]) return dp[n];
  if(n<=1) return n; 
  else return dp[n] = fib(n-1)+fib(n-2);
} 

int main() { 
  printf("%d", fib(35)); 
}