求一算法

zhufeiguanghui 2008-12-16 09:33:08


含N个元素的数组,现在要任意取出M个元素,有没有什么算法可以把所有情况都列出来啊?用C/C++
...全文
145 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
meihua8131634828 2009-03-08
  • 打赏
  • 举报
回复
2. 一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。
zhufeiguanghui 2008-12-16
  • 打赏
  • 举报
回复
谁有这个算法啊????
bfhtian 2008-12-16
  • 打赏
  • 举报
回复
应该是n!/(m!*(n-m)!)吧
GreaterHeat 2008-12-16
  • 打赏
  • 举报
回复
LZ问的是一排列组合问题?
列出N个元素中取出M个元素的所有组合?
用递归应该是可以的.
P(N,M)总共是 N!/M!种咯.
zhufeiguanghui 2008-12-16
  • 打赏
  • 举报
回复
怎么用递归啊?
jieao111 2008-12-16
  • 打赏
  • 举报
回复
排列问题嘛,用递归吧
xuruichen 2008-12-16
  • 打赏
  • 举报
回复
怎么个任意法?怎么随机?有公式吗?
ARLENE_YANG 2008-12-16
  • 打赏
  • 举报
回复
递归算法应该可以的
zhufeiguanghui 2008-12-16
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 whsfer 的回复:]
这个算法也就是求M <3的吧,再多的话建议用递归算法,能节省很多时间。
[/Quote]

递归算法对于数多的情况效率也不高啊???
zhufeiguanghui 2008-12-16
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 whsfer 的回复:]
我就来个非递归的吧。
#include <iostream>
using namespace std;
int count=0;
void Getcount(int a[],int m,int n)
{
for(int i=0;i <= m-n;i++)
{
for(int j = i+1;j <m;j++)
{
for(int k=i+2;k < m;k++)
{
if(k>j)
{
count++;
cout < <a[i] < <" " < <a[j] < <" " < <a[k] < <endl;
}
}
}
}
cout < <count < <endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[…
[/Quote]

13楼你的多重循环办法只能对于数少的情况好用一点,对于数多一点的话,那效率就低下了,有没有其他的办法?
whsfer 2008-12-16
  • 打赏
  • 举报
回复
这个算法也就是求M<3的吧,再多的话建议用递归算法,能节省很多时间。
whsfer 2008-12-16
  • 打赏
  • 举报
回复
我就来个非递归的吧。
#include <iostream>
using namespace std;
int count=0;
void Getcount(int a[],int m,int n)
{
for(int i=0;i <= m-n;i++)
{
for(int j = i+1;j<m;j++)
{
for(int k=i+2;k< m;k++)
{
if(k>j)
{
count++;
cout<<a[i]<<" "<<a[j]<<" "<<a[k]<<endl;
}
}
}
}
cout<<count<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[]={1,2,3,4,5,6,7,8};
Getcount(a,8,3);
return 0;
}
zhufeiguanghui 2008-12-16
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lala9517 的回复:]
C/C++ code#include <stdio.h>
#define MAXN 100
int b[MAXN];
int counts=0;

void comb(int a[], int m, int n)
{
int i,j;
for (i=m;i>=n;i--)
{
b[n]=a[i-1];
if (n>1)
comb(a, i-1, n-1);
else
{
counts++;
for (j=b[0];j>0;j--)
printf("%4d",b[j]);
printf("\n");
}
}
}…
[/Quote]




如果不用递归的话,用什么方法比较好啊?
lala9517 2008-12-16
  • 打赏
  • 举报
回复
#include <stdio.h>
#define MAXN 100
int b[MAXN];
int counts=0;

void comb(int a[], int m, int n)
{
int i,j;
for (i=m;i>=n;i--)
{
b[n]=a[i-1];
if (n>1)
comb(a, i-1, n-1);
else
{
counts++;
for (j=b[0];j>0;j--)
printf("%4d",b[j]);
printf("\n");
}
}
}

int main()
{

int m = 3, r = 2;
int a[] = {33,22,11};
counts=0;
b[0]=r;
comb(a,m,r);
printf("%d\n", counts);
return 0;
}


不是我写的,直接从网上弄下来改了改...
直接百度,就能找出好多来嘛...
sw1024 2008-12-16
  • 打赏
  • 举报
回复
递归,大脑第一反应就是递归,很简单的
herman~~ 2008-12-16
  • 打赏
  • 举报
回复
cin >> Ncount;
for(i = 0; i < Ncount; i++
arr[rondom()% arrcout];
fqblog 2008-12-16
  • 打赏
  • 举报
回复
程序员联盟群: (73204313)

主要讨论:c/c++,开源技术,linux,window等.
欢迎编程爱好者加入... ...

65,211

社区成员

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

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