Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <time.h>
- #include <stdio.h>
- #include <time.h>
- // dynamic programming
- /*
- * f(a,b) : phan tich b thanh tong cac so nguyen <=a
- * Neu a > b --> khong the co so a : f(a-1,b)
- * Neu a<= b --> co/ khong co so a : f(a,b-a) + f(a-1,b)
- * base : if (b==0) : return 0. (a==0) : return 0
- */
- int f(int a, int b){
- // base case
- if (b==0)
- return 1;
- if (a ==0)
- return 0;
- //recursion case
- if (a>b)
- return f(a-1,b);
- else
- return f(a,b-a) + f(a-1, b);
- }
- int main(){
- int n = 5;
- int res = f(n, n);
- printf("%d\n",res);
- }
Advertisement
Add Comment
Please, Sign In to add comment