我们继续来看递归调用的例子。
BNUOJ4118
Input
输入 只有一个数字N (0<8)
Output
输出 由1~N这N个数字组成的全排列,按从小到大的顺序。
#include <stdio.h>
#include <set>
using namespace std;
set<int> used;
int n,k,Flag=0;
int tt[20];
void di(int pt)
{
int i,flag;
if (pt>k)
{
for(i=1; i<=k; ++i)
{
if(i>=2)
{
used.insert(tt[i-1]);
if(used.find(tt[i]) != used.end())
{ flag=1;break;}
}
if(i==k)
{ for(i=1; i<=k; ++i)
{
cout << tt[i] ;
}
++Flag;
}
}
if(flag!=1)
cout << endl;
}
else
{
for(i=1;i<=n;++i)
{
tt[pt] = i;
if(i>=2)
used.clear();
di(pt+1);
}
}
}
int main()
{
cin >> n;
k=n;
di(1);
return 0;
}
下面是优化的解答
#include <iostream>
#include <string.h>
using namespace std;
int n, num[10];
bool used[10];
void di(int p)
{
if(p > n)
{
for(int i = 1; i <= n; i++)
cout << num[i] ;
cout << endl ;
return;
}
for(int i = 1; i <= n; i++)
if(used[i] == false)
{
num[p] = i;
used[i] = true;
di(p + 1);
used[i] = false;
}
}
int main()
{
cin >> n ;
memset(used, false, sizeof(used));
di(1);
return 0;
}
最后看另一道类似的题目
BNUOJ1108
Input
The input will contain one or more test cases. Each test case consists of one line containing several integers separated from each other by spaces. The first integer on the line will be the number k (6 < k < 13). Then k integers, specifying the set S, will follow in ascending order. Input will be terminated by a value of zero (0) for k.
Output
For each test case, print all possible games, each game on one line. The numbers of each game have to be sorted in ascending order and separated from each other by exactly one space. The games themselves have to be sorted lexicographically, that means sorted by the lowest number first, then by the second lowest and so on, as demonstrated in the sample output below. The test cases have to be separated from each other by exactly one blank line. Do not put a blank line after the last test case.
#include <iostream>
#include <string.h>
using namespace std;
int num[20];
int int_set[20];
bool used[20];
void di(int n, int p)
{
if(p > 6)
{
for(int i = 1; i < 6; i++)
cout << num[i] << " ";
cout << num[6]; //注意这里若不这样写,输出格式就不对,num[6]后面不能有空格
cout << endl ;
return;
}
for(int i = 1; i <= n; i++)
if(used[i] == false && num[p - 1]< int_set[i])
{
num[p] = int_set[i];
used[i] = true;
di(n, p + 1);
used[i] = false;
}
}
int main()
{
int n, i, flag = 0;
while(cin >> n && n != 0)
{
if(flag)
cout << endl ;
flag = 1;
memset(used, false, sizeof(used));
for(i = 1; i <= n; i++)
cin >> int_set[i] ;
di(n, 1);
}
return 0;
}
以上算法的基本思想大同小异,其中有一些技巧尤其值得关注。
另外,由于我是初学者,我在写这些代码的过程中学到了一下零星的内容:
1.阶乘递归
#include <iostream>
using namespace std;
int recv(int n)
{
int sum = 1;
if(1 == n)
{
return 1;
}
sum =n * recv(n - 1);
return sum;
}
int main()
{
int num = 0;
cin >> num;
cout << recv(num) << endl;
return 0;
}
2.指向数组的指针
函数调用中,如果需要传入数组参数,一般是写指向数组的指针。例如
memset(used, false, sizeof(used));
中的used就是一个指向数组的指针。
3.函数的调用
通过以上例子不难学习如何进行函数调用。
4.函数的return
函数的返回值,和函数定义的类型相关,特别的void不返回值,默认返回0值,可用于检验。
5.跳出多重循环
1)goto
2)flag标记执行
5.memset()函数
参见http://www.cnblogs.com/pengjun-shanghai/p/4807354.html
6.set命令
#include <iostream>
#include <set>
using namespace std;
set<int> S;
int main()
{
int n, m;
cout << "输入插入元素个数:" << endl;
cin >> n;
for(int i = 0; i < n; i++)
{
int t;
cin >> t;
S.insert(t);
}
cout << "输入询问次数:" << endl;
cin >> m;
for(int i = 0; i < m; i++)
{
int t;
cin >> t;
if(S.find(t) != S.end())
cout << "ok" << endl;
else
cout << "no" << endl;
}
return 0;
}
7.bool用以标记
参见上面几个例子,里面用到了bool.