用递推做这道题

fdg个sggggg 2008-10-04 09:37:23
已知Cmn表示从m个元素中取n个数的组合数,又知
Cmn=Cm-1n+Cm-1n-1
Cmn=1(m=n),Cmn=m(n=1)
谁能告诉我用递推怎吗做这道题啊?
...全文
184 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yshuise 2008-10-04
  • 打赏
  • 举报
回复
运行数度最快的程序:
include <iostream>
#include <stdlib.h>

using namespace std;
template<int T, int N> struct f{
enum {ret = f<T-1,N>::ret + f<T-1,N-1>::ret};
};
template <int T>
struct f<T, T>{
enum {ret = 1};
};
template <int T>
struct f<T, 1>{
enum {ret = T};
};
template <int N>
struct f<1,N>{
enum {ret = N };
};

int main(int argc, char *argv[])
{
//cout<<f<4,4>::ret<<endl;
cout<<f<2,3>::ret<<endl;
system("PAUSE");
return 0;
}
fdg个sggggg 2008-10-04
  • 打赏
  • 举报
回复
不是递归迭代也可以,老师让用法递推。
就呆在云上 2008-10-04
  • 打赏
  • 举报
回复
貌似条件不够哦,我添加了一个不知道是不是你想要的

/***********************************************************************
/* Cmn=Cm-1n+Cm-1n-1
Cmn=1(m=n),Cmn=m(n=1)
/************************************************************************/


#include <stdio.h>

int f(int x, int y) {
if (x == y)
{
return 1;
}
if (y == 1)
{
return x;
}
//应该要添加一个条件啊
if (x == 1)
{
return y; //返回值有条件决定
}
return f(x-1, y) + f(x-1, y-1);
}

int main ( ) {
printf(" %d \n", f(2, 3));
}
zhyinty 2008-10-04
  • 打赏
  • 举报
回复
你是说迭代还是递归???
jia_xiaoxin 2008-10-04
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

int cmn(int m,int n)
{
if(n==1)
return m;
if(n==m)
return 1;
return cmn(m-1,n-1)+cmn(m-1,n);
}
int main()
{
cout<<cmn(10,3)<<endl;
return 0;
}


fdg个sggggg 2008-10-04
  • 打赏
  • 举报
回复
这不是递归吗,要用递推。
rivulettornado 2008-10-04
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

int choose(int m,int n);

int main()
{
int m,n;
cout<<"m = ";cin>>m;
cout<<"n = ";cin>>n;
cout<<"Result = "<<choose(m,n)<<endl;
system("pause");
}
int choose(int m,int n)
{
if(m==n) return 1;
else if(n==0) return 1;
else if(n==1) return m;
else return (choose(m-1,n-1)+choose(m-1,n));
}

guizi110 2008-10-04
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;


int main()
{
int c[100][100];
int m,n;
cout<< "输入m(小于100):" ;
cin >> m;
cout<< "输入n(小于100):" ;
cin >> n;
for(int im=1; im<=m; im++)
for(int in=1; (in <=n)&&(im >=in); in++)
{
if (1 == in)
{
c[im][in]=im;
cout<< "C" << im << in << " = "<< c[im][in] << endl;
}
else
{
if (in == im)
{
c[im][in]=1;
cout<< "C" << im << in << " = "<< c[im][in] << endl;
}
else
{
c[im][in] = c[im-1][in]+c[im-1][in-1];
cout<< "C" << im << in << " = "<< c[im][in] << endl;
}
}
}

}
野男孩 2008-10-04
  • 打赏
  • 举报
回复
求求你了!递推!不要整天想着显摆编译时计算。

[Quote=引用 7 楼 yshuise 的回复:]
运行数度最快的程序:

C/C++ codeinclude <iostream>
#include <stdlib.h>

using namespace std;
template<int T, int N> struct f{
enum {ret = f<T-1,N>::ret + f<T-1,N-1>::ret};
};
template <int T>
struct f<T, T>{
enum {ret = 1};
};
template <int T>
struct f<T, 1>{
enum {ret = T};
};
template <int N>
struct f<1,N>{
enum {ret = N };
};

int main(int argc, char *argv[])
{

[/Quote]

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧