您的位置:首页 > 其它

递归2:递归的一般解题思路

2018-05-24 10:46 155 查看

编程题目适合用递归,但是接口函数中的传参无法用递归时,利用自定义的

printHelper(    ,     )来实现递归。


递归的一般思路

___ printHelper()
{
if( baseline )      //递归返回条件
{
return ;
}else{

chose;         //选择
explor;        //递归操作
unchuse;       //还原

}
}

1.打印所有二进制

思路:

n位二进制,每一位都有选0、选1的可能

递归返回:选够n位

/---------------------------递归一般思路,传递引用---------------------------/

char temp[]={"0","1"};                        //string.push_back() 只能放单个字符

void printHelper(int &n, string &s)          //n位二进制,第一次穿的s是空串,用来存结果,
{                                            //(s 为传递引用,影响上一层递归结果)

if(s.size() == n)                             //baseline
cout<< s << endl;
else{
for(int i=0; i < 2; i++)
{
s.push_back(temp[i]);             //chose printHelper(n,s);
printHelper(n,s);                 //exploer
s.pop_back();                     //unchose
}
}
}

/-------------------------------传递值---------------------------------/

string temp[]={"0","1"};                      //两个字符串相加

void printHelper(int &n, string s)            //n位二进制,第一次穿的s是空串,用来存结果
{                                             //(s 为值传递,不影响上一层递归)
if(s.size() == n)
cout<< s << endl;
else{
for(int i=0; i < 2; i++)
{
s+=temp[i];                       //两个字符串相加
printHelper(n,s);
s.pop_back();                     //不还原的话,第三层 000 返回后,i为1时成 0001
}
}
}

2.打印全排列


思路:

abc的全排列,分为a—per(bc)   b—per(ac)   c—per(ab)三种


递归返回:只有一个元素

/---------------------------递归一般思路,传递引用---------------------------/
void printHelper(vector<char> &s, vector<char> &v, set<string> &rec)
{
if(s.size() == 0);
{
string stemp = "";
for(int i=0; i < v.size(); i++)
stemp.push_back(v[i]);

rec.insert(step);
}else
{
for(int i=0; i < s.size(); i++)       //转换成下一个数量级,
{                                     //s若为abc,每次循环分别为 bc ac ab
v.push_back(s[i]);
char temp =s[i];
s.erase(s.begin()+i);

printHleper(s,v,rec);

v.pop_back();
s.insert(s.begin()+i; temp);

}
}

}

/-----------------------------传递值,不需 unchose---------------------------/
template<class T>
void swap( T &a, T &b)
{
T temp = a;
a = b;
b = temp;
}

template<class T>
void permulateNumber(T list[], int k, int m)      //list为要排列的数组
{                                                 //k(数组起始下标),m(数组终点下标)
if(k == m)
{
for(int i=0; i < m; i++)
{
cout << list[i];
}
cout << endl;
}else
{
for(int i=k; i < m; i++)                      //
{
swap(list[k],list[i]);
permulateNumber(list, k+1, m);             //数量减小(k值变化)
swap(list[i],list[k]);                     //注意还原顺序
}
}

}

3.对序列找下标

[p]序列为: a, aa, aaa, aaaa,  aaab......aaay........yyyy,a的下标为0,aa为1,以此类推

输入:要找的字符串 b

输出:直到 b 出现的序列, b串下标

#include <iostream>
using namespace std;
#include <string>
#include <sstream>

int c = -1;
bool on = true;                              //用于控制找到指定字符串返回
string* s = new string[25];

void findIndex(string a,string b){            //a为递归输入,b是要找的字符串
//cout << a <<endl;                   //可查看on 为false后的递归返回
if(on)
{
cout << a <<endl;
if(a.compare(b) == 0)
{
cout << c <<endl;
on = false;              //控制只输出到  b
return;
}
else
{
c++;
if(a.size() == 4)
return ;
else
{
for(int i=0; i < 25; i++)
{
a += s[i];
findIndex(a,b);
a.pop_back();
}
}
}
}
}

int main(){

for (int i = 0; i < 25; i++){
char c = (char)(i + 96 + 1);
string s1;
stringstream ss;
ss << c;
ss >> s1;
s[i] = s1;
}

string str;
cin >> str;
findIndex("",str);
system("pause");
return 0;
}

4.RollingDice

输入: 筛子个数 n,筛子数总和 sum[/p]

输出:所有的筛子数组合

#include< iostream>
#include< string >
#include< vector >
#include< numeric >

using namespace std;

int n,sum;

void RollingDice(vector <int > &vec)
{
if( vec.size() == n)
{
int v_sum=0;

v_sum = accumulate(vec.begin(),vec.end(),0);
if(v_sum == sum)
{
for(int i=0; i < n; i++)
cout<<vec[i]<< ' ';
cout<<endl;
}
return ;
}else
{
for(int i=1; i < 7; i++)
{
vec.push_back(i);
RollingDice(vec);
vec.pop_back();
}
}
}
int main()
{
n=3;
sum=13;
vector< int > vec;
RollingDice(vec);

system("pause");
return 0;
}



阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: