数据结构和算法的一道习题,请高手指点。

tsao 2010-08-15 08:50:31
试编写一个递归函数,用来输出n个元素的所有子集。例如,三个元素{a, b, c} 的所有
子集是:{ }(空集),{a}, {b}, {c}, {a, b}, {a, c}, {b, c} 和{a, b, c}。
...全文
310 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanchenyu 2010-08-31
  • 打赏
  • 举报
回复
看看,学习
kenshin3131985 2010-08-27
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 michaelbomb 的回复:]
引用 10 楼 chonet2009 的回复:
这样写通用些,char str[N] = "1d8";中可为任意串。 要注意的是 N 大小包括了'\0'


C/C++ code

#include "stdafx.h"
#include <stdio.h>

#define N 4

void fun(char * str, bool *bmap, int n)
{
i……
[/Quote]

简单言之,让调用的FUN循环到n=3,再把bmap[2]置1,打印出来,return负责跳出当前调用,然后依次n=2,1,大体上就是这样的一个过程
PS: 你可以在VS里调试一下,就知道了
MichaelBomb 2010-08-27
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 chonet2009 的回复:]
这样写通用些,char str[N] = "1d8";中可为任意串。 要注意的是 N 大小包括了'\0'


C/C++ code

#include "stdafx.h"
#include <stdio.h>

#define N 4

void fun(char * str, bool *bmap, int n)
{
if( n == N - 1 )
……
[/Quote]

楼上的算法 怎么理解。
眼睛猥琐男 2010-08-27
  • 打赏
  • 举报
回复
这样写通用些,char str[N] = "1d8";中可为任意串。 要注意的是 N 大小包括了'\0'


#include "stdafx.h"
#include <stdio.h>

#define N 4

void fun(char * str, bool *bmap, int n)
{
if( n == N - 1 )
{
printf("{ ");

for (int j = 0; j < N -1; j++)
{
if (bmap[j])
{
printf("%c,",str[j]);
}
}

printf("\b }\n");

return;

}

bmap[n] = 0;
fun(str, bmap, n + 1);
bmap[n] = 1;
fun(str, bmap, n + 1);
}

void main()
{
char str[N] = "1d8";
bool bmap[N] = {0};
fun(str, bmap, 0);
}
lymastee 2010-08-22
  • 打赏
  • 举报
回复
从0 - n地枚举所有组合,
至于格式的问题, 可以使用template, 也可以自己用type_id实现....
suchx 2010-08-22
  • 打赏
  • 举报
回复

#include "stdafx.h"
#define maxsize 4
void select(char a[],int n, int size)
{ int i;
if(n==maxsize)
{ printf("{");
for(i=0;i<size;i++)
{ printf("%2c",a[i]);
}
printf("}");
printf("\n");
}
else
{ select(a,n+1,size);
a[size++]='a'-1+n;
select(a,n+1,size);
size--;
}
}
int _tmain(int argc, _TCHAR* argv[])
{ char a[maxsize]={0};
int size=0;
select(a,1,size);
return 0;
}
奔跑的蜗牛 2010-08-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xiazdong 的回复:]

格式怎么办呀,,为什么不能调整的。。??
[/Quote]
加这个试试
xiazdong 2010-08-20
  • 打赏
  • 举报
回复
格式怎么办呀,,为什么不能调整的。。??
xiazdong 2010-08-20
  • 打赏
  • 举报
回复
上面没格式,我重发一份
#include "stdafx.h"
#include<iostream>
using namespace std;
const int SIZE=4;
int _tmain(int argc, _TCHAR* argv[])
{
char ch[4]={'a','b','c','d'};
for(int i=0;i<SIZE;i++)
{
cout<<ch[i]<<" ";
}
cout<<endl;
for(int i=0;i<SIZE;i++)
{
for(int j=i+1;j<SIZE;j++)
{
cout<<ch[i];
cout<<ch[j];
for(int k=j+1;k<SIZE;k++)
{
cout<<ch[k];
}
cout<<endl;
}
}
cin.get();
return 0;
}
xiazdong 2010-08-20
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include<iostream>
using namespace std;
const int SIZE=4;
int _tmain(int argc, _TCHAR* argv[])
{
char ch[4]={'a','b','c','d'};
for(int i=0;i<SIZE;i++)
{
cout<<ch[i]<<" ";
}
cout<<endl;
for(int i=0;i<SIZE;i++)
{
for(int j=i+1;j<SIZE;j++)
{
cout<<ch[i];
cout<<ch[j];
for(int k=j+1;k<SIZE;k++)
{
cout<<ch[k];
}
cout<<endl;
}
}
cin.get();
return 0;
}
dreamworld_sh 2010-08-15
  • 打赏
  • 举报
回复
另外一种方法是否也可行,n=1,2,3代表输出个数
Erorr 2010-08-15
  • 打赏
  • 举报
回复
如果n=1,输出a
如果n=2,追加输出b,ab
如果n=3,追加输出c,ac,bc,abc,
如果n=4,追加输出d,ad,bd,abd,cd,acd,bcd,abcd
就是输出增加的字母,并且在以前输出的字母上都加上这个字母

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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