华为OJ:递归分苹果
2016-08-18 21:13
489 查看
题目描述把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。 输入每个用例包含二个整数M和N。0<=m<=10,1<=n<=10。<=n<=10<=m<=10 样例输入7 3 样例输出8 /** * 计算放苹果方法数目
* 输入值非法时返回-1 * 1 <= m,n <= 10<><= m,n <= 10<> * @param m 苹果数目 * @param n 盘子数目数 * @return 放置方法总数 * */ public static int count(int m, int n) //递归递归大递归
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#include<iostream>
#include<cstdio>
#define _CRT_SECURE_NO_DEPRECATE
using namespace std;
int a[15][15];
int n, m;
//递归
int f(int n, int m)
{
if (n<0)return 0;
if (n == 0 || m == 1) return 1;
return f(n, m - 1) + f(n - m, m);//有0和无0
}
int main()
{
int n, m;
cin >> n;
cin >> m;
cout<< f(n, m);
system("pause");
return 0;
}
* 输入值非法时返回-1 * 1 <= m,n <= 10<><= m,n <= 10<> * @param m 苹果数目 * @param n 盘子数目数 * @return 放置方法总数 * */ public static int count(int m, int n) //递归递归大递归
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#include<iostream>
#include<cstdio>
#define _CRT_SECURE_NO_DEPRECATE
using namespace std;
int a[15][15];
int n, m;
//递归
int f(int n, int m)
{
if (n<0)return 0;
if (n == 0 || m == 1) return 1;
return f(n, m - 1) + f(n - m, m);//有0和无0
}
int main()
{
int n, m;
cin >> n;
cin >> m;
cout<< f(n, m);
system("pause");
return 0;
}