回复 10楼 beyondyf
确实是这样的 杨大哥说的不错 又学习了所以楼主的题目是不能打印的 最多只能求个总数
===========深入<----------------->浅出============
#include <stdio.h> #include <string.h> int f(int m,int n) { if(m<0) return 0; if(m==0||n==1) return 1; return f(m-n,n)+f(m,n-1); } int main() { int t; scanf("%d",&t); int m,n; while(t--) { scanf("%d%d",&m,&n); printf("%d\n",f(m,n)); } return 0; } /* 最少的盘子放了一个,这样每个盘子至少一个,n个盘子先放上n个,剩下的m-n个可以随便放 最少的盘子没有放,这样剩下的n-1个盘子还是随便放m个 */
#include <stdio.h> int fun(int n, int m) { int i; int sum = 0; if (m <= 0 || n <= 0) return 0; if (m == 1 || n == 1) return 1; if (m == 2) return n; if (m > 2 && n > 1) { for(i = 0; i <= m - 1; i++) { sum += fun(n - 1, m - i); } return sum; } } int main() { int m, n; printf("please input n & m: "); while(EOF != scanf("%d%d", &n, &m)) printf("the result is %d\n", fun(n, m)); return 0; }
#include <stdio.h> #include <time.h> #include <windows.h> #include <dos.h> double fun(int n, int m) { int i; double sum = 0; if (m <= 0 || n <= 0) return 0; if (m == 1 || n == 1) return 1; if (m == 2) return n; if (m > 2 && n > 1) { for(i = 0; i <= m - 1; i++) { sum += fun(n - 1, m - i); } return sum; } } int main() { int m, n; double sum; float start, end; printf("please input n & m: "); while(EOF != scanf("%d%d", &n, &m)) { start = clock(); sum = fun(n, m); printf("the result is %lf\n", sum); end = clock(); printf("time = %3.3f\n", (end-start)/CLK_TCK); } return 0; }