你用什么算法?

lupenda 2003-10-08 10:05:03
读取字符串,输出它们的所有组合
...全文
69 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
12s 2003-11-25
  • 打赏
  • 举报
回复
我的用纯循环解决!!不过可读性不怎么样!!

#include <stdio.h>
#include <stdlib.h>

#define MAX 5

char str[MAX]={'a','b','c','d','e'};

void c(char *string)
{
int i,j,k;
for(i=0;i<MAX;i++) { /*当前打头字符*/
printf("%c\t",string[i]);
for(j=i+1;j<MAX;j++) { /*当前字符数 如:ABC为3个字符*/
printf("%c",string[i]); /*当前开头字母*/
for(k=i+1;k<=j;k++)
printf("%c",string[k]); /*后序字母*/
printf("\t");
}
printf("\n");
}
}

void main()
{
clrscr();
c(str);
getch();
}
12s 2003-11-25
  • 打赏
  • 举报
回复
怎么有重复的贴子啊?前面不是有一个排列组合的了吗?
fjirie 2003-11-24
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;

void permutation_aux(string front, string w) {
if (w.size() <= 1)
cout<<front+w<<endl;
else {
for(string::iterator p= w.begin(); p < w.end(); p++) {
char temp= *p;
w.erase(p);
permutation_aux(front+temp, w);
w.insert(p, temp);
}
}
}

void Permutation(string s) {
permutation_aux("", s);
}

int main()
{
Permutation("ABCD");
}
ZhangYv 2003-11-23
  • 打赏
  • 举报
回复
你把输入改改就可以了:
#include <stdio.h>
#include <stdlib.h>
const int MAXSIZE = 80;
int N,M;
int A[MAXSIZE], B[MAXSIZE];
void Print()
{
int i;
for (i = 0; i < M; ++i)
printf("%d ", B[i]);
printf("\n");
}

void Com(int n, int m)
{
int i;
for (i = n; i >= m; --i){ //抽屉原理
B[m] = A[i];
if (m > 0)
Com(i-1,m-1);
else
Print();
}
}
int main()
{
int i;
/* printf("%s", "Input N M = ");
scanf("%d%d", &N,&M);
*/
N = 5;
M=3;
if (N > MAXSIZE)
return 1; //越界
for (i = 0; i < N; ++i)
A[i] = i+1;
Com(N-1,M-1);
system("pause");
return 0;
}
lupenda 2003-11-23
  • 打赏
  • 举报
回复
顶!
cafeeee 2003-10-08
  • 打赏
  • 举报
回复
??
什么意思?
是把字符串重新排列吗?
那样的话就是一个排列生成算法了。
Andy84920 2003-10-08
  • 打赏
  • 举报
回复
好像有专门的算法!

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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