RSS Feed

全排列

2006-11-17 by bborn

题目:写一个函数, 如 Foo(const char *str), 打印出 str 的全排列,
如 abc 的全排列: abc, acb, bca, dac, cab, cba

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include “stdafx.h”
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace ::std;
void Foo(const char *str);           //stl
void Foo2(const char *str);          //recursion
void foo2( char *str,int position,int strlen ,int &count);
void Foo3(const char *str);          //loop
int main(int argc, char *argv[])
{
unsigned long start = ::GetTickCount();
Foo(”abcdefg”);
unsigned long end = ::GetTickCount();
unsigned long worktime1 = end - start;
start = ::GetTickCount();
Foo2(”abcdefg”);
end = ::GetTickCount();
unsigned long worktime2 = end - start;
start = ::GetTickCount();
Foo3(”abcdefg”);
end = ::GetTickCount();
unsigned long worktime3 = end - start;
cout< <”use stl time =<<worktime1 <<endl;
cout<<”use recursion time =<<worktime2 <<endl;
cout<<”use loop time =<<worktime3 <<endl;
system(”pause”);
return 0;
}
void Foo(const char *str)
{
int n=strlen(str),count=0;
vector<char> a(n);
for(int i=0; i<n ; i++)
a[i]=str[i];
vector<char>::iterator start, end;
ostream_iterator<char> outIt(cout, “”);
start = a.begin();
end = a.end();
do {
count++;
copy(start, end, outIt) ;
cout < < endl ;
} while(next_permutation(start, end));
cout<<count<<endl;
}
void Foo2(const char *str)
{
int n=strlen(str),count=0;
char *mystr = (char *)malloc(n*sizeof(char));
memcpy(mystr , str ,n);
 
foo2(mystr , 0 , n ,count);
cout<<count<<endl;
free(mystr);
}
void foo2(char *str,int position,int strlen ,int &count)
{
if(position == strlen)
{
cout<<str<<endl;
count ++;
}
else
{
for(int i = position; i < strlen; i++)
{
swap(*(str + i) , *(str + position));
foo2(str,position+1,strlen,count);
swap(*(str + i) , *(str + position));
}
}
}
void Foo3(const char *str)
{
int len = strlen(str);
int * range = new int[len];
int cont, i, j, counter = 0;
memset(range, 0, len*sizeof(int));
while(1)
{
for(i=0; i<len; i++)
{
if(++range[i]>=len)
range[i] = 0;
else
break;
}
if (i==len)
{
break;
}
for(i=0,cont=0; i<len -1; i++)
{
for(j=i+1; j<len; j++)
{
if(range[i] == range[j])
{
cont=1;
break;
}
}
if (cont == 1)
{
break;
}
}
if (cont) continue;
for(i=0; i<len; i++)
{
printf(%c”, str[range[i]]);
}
printf(”n”);
++counter;
}
delete [] range;
cout<<counter<<endl;
}

据说是迅雷的一个笔试题目,这给了三种方法,第一种用库函数,当然这是一个玩笑.第二使用的递归,第三使用的是循环,第三个的思路比较特别,生成一个不重复的数列,一般的比较,递归是比较快的一种方法,其次是循环.这些代码都是来自csdn的讨论,我做了些修改和优化.还有,在stdafx.h中加入 #include "Windows.h",没有考虑有重复字符的问题

可能相关


没有评论 »

还没有评论呢。

发表评论

电子邮件地址不会被公开。

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">