hqt

Simple Dynamic Programming

hqt
Jul 29th, 2012
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.59 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4.  
  5. // dynamic programming
  6. /*
  7.  * f(a,b) : phan tich b thanh tong cac so nguyen <=a
  8.  * Neu a > b --> khong the co so a : f(a-1,b)
  9.  * Neu a<= b --> co/ khong co so a : f(a,b-a) + f(a-1,b)
  10.  * base : if (b==0) : return 0. (a==0) : return 0
  11.  */
  12. int f(int a, int b){
  13.     // base case
  14.     if (b==0)
  15.         return 1;
  16.     if (a ==0)
  17.         return 0;
  18.     //recursion case
  19.     if (a>b)
  20.         return f(a-1,b);
  21.     else
  22.         return f(a,b-a) + f(a-1, b);
  23. }
  24.  
  25. int main(){
  26.     int n = 5;
  27.     int res = f(n, n);
  28.     printf("%d\n",res);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment