# 请教一个较难面试题（穷举）

yangbo76 2008-03-12 10:51:43

...全文
132 10 打赏 收藏 转发到动态 举报

10 条回复

yangbo76 2008-03-13
• 打赏
• 举报

next_permutation 确实好用，连重复排列都不用操心。

sort(str.begin(),str.end());
do {
if( foundInDictionary( str, dictfile ))
cout << str << endl;
} while( next_permutation ( str.begin(), str.end()));

jinwei1984 2008-03-12
• 打赏
• 举报

qiucp 2008-03-12
• 打赏
• 举报

#include <stdio.h>
const int N = 9;
int Hui(int* p, int num, int have);
int main(int argc, char* argv[])
{
int p[N];
int n;
printf("Input n(n<=9):");
scanf("%d", &n);
int r = Hui(p, n, 0);
printf("Total %d\n", r);
return 0;
}

// *p:数组;num:数组个数;have:已经装入数组的个数
int Hui(int* p, int num, int have)
{
int r = 0;
if (have == num)
{
for (int i = 0; i < num; i++)
{
printf("%d", p[i]);
}
printf("\n");
r = 1;
}
else
{
for (int i = 1; i <= num; i++)
{
bool b = false;
for (int j = 0; j < have; j++)
{
if (p[j] == i)
{
b = true;
break;
}
}
if (!b)
{
p[have] = i;
r += Hui(p, num, have + 1);
}
}
}
return r;
}
AmHuman 2008-03-12
• 打赏
• 举报

billy1985 2008-03-12
• 打赏
• 举报

p0303230 2008-03-12
• 打赏
• 举报

p0303230 2008-03-12
• 打赏
• 举报

``````
public class stackout{
private long fact(int n)
{
if(n == 0)
return 1;
return fact(n - 1)*n;
}
private final long M(final int n)
{
return (fact(2*n))/((n + 1)*fact(n)*fact(n));
}
private static char allchar[] = {'1','1', '2','3','4'};//'a'+7,'a','b'+7,'b','c'+7,'c',
//'d'+7,'d','e'+7,'e'};
private static final int  N = allchar.length;
private char outchar[] = new char[N];
private boolean op[] = new boolean[2*N];
private int count = 0;
private String[] out_st = new String[(int) M(N)];
public void k(int all, int in, int out)
{
if(all > 0)
{
op[out] = true;
k(all - 1, in + 1, out + 1);
if(in > 0)
{
op[out] = false;
k(all, in - 1, out + 1);
}
}else{
out_st[count] = "";
boolean repeat = false;
int   spos = 0;
int   cpos = 0;
for(int i = out; i < 2*N; i++)
{
op[i] = false;
}
//   打印出栈顺序
for   (int i=0;   i< N*2;   i++)   {
if   (op[i])
{
outchar[spos++]   =  allchar[cpos++];
}
else   {
out_st[count] += outchar[--spos];
//System.out.print(outchar[--spos]);
}
}
for(int i = 0; i < count; i++)
{
if(out_st[count].equals(out_st[i]))
{
repeat = true;
break;
}
}
if(!repeat)
{
System.out.print((1+count)  + ",");
System.out.print(out_st[count]);
System.out.print("\n");
count++;
}
}
}
public static void main(String[] args){
stackout test = new stackout();
//System.out.println(test.M(11));
test.k(N, 0, 0);
}
}``````

freshui 2008-03-12
• 打赏
• 举报

p0303230 2008-03-12
• 打赏
• 举报

wpalhm 2008-03-12
• 打赏
• 举报

#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>

typedef std::string::size_type size_t;

void range(std::string& str, size_t beg, size_t end)
{
if( beg == end )
std::cout << str << std::endl;
else
for( size_t i(beg); i!=end; ++i )
{
std::swap( str.at(i), str.at(beg) );
range(str, beg+1, end);
std::swap( str.at(i), str.at(beg) );
}
}

int _tmain(int argc, _TCHAR* argv[])
{
std::string str = "abcde";
range( str, 0, str.size() );

std::cin.get();
return 0;
}

64,312

c++ 技术论坛（原bbs）

• 近7日
• 近30日
• 至今

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下