以前一下就写出来,今天花了一下午。想死的心都有了,
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
vector<int> vec;
char s[] = "abcd";
static int k = 0;
void pailie(char *s,int start,int end)
{
if (start == end)
{
cout << s << endl;
return;
}
else
{
for (int i = start ; i <= end ; i++)
{
char temp = s[start];
s[start] = s[i];
s[i] = temp;
pailie(s,start + 1,end);
temp = s[start];
s[start] = s[i];
s[i] = temp;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
pailie(s,0,3);
return 0;
}
本来是在做《剑指offer》从1到n位数的所有数的。
#include "stdafx.h"
#include <iostream>
using namespace std;
const int MAX_SIZE = 10000;
char s[MAX_SIZE];
void PrintNum(char *s,int size)
{
for (int i = 0; i < size ; i++)
{
cout << s[i] ;
}
cout << endl;
}
void changeBit(char *s,int index,int n)
{
if (index == n)
{
PrintNum(s,n);
return;
}
for (int i = 0 ; i <= 9 ; i++)
{
s[index] = '0' + i;
changeBit(s,index + 1, n);
}
}
void print(int n)
{
//int i = 0;
char *s = new char[n];
changeBit(s,0,n);
}
int _tmain(int argc, _TCHAR* argv[])
{
print(4);
return 0;
}
我还是把我错误的全排列代码列出来,当时因为去想换元素之前做一次全排列递归,换一次之后又做一次结果肯定会有很多重复的,然而一直不知道怎么去更改。
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
vector<int> vec;
char s[] = {'a','b','c'};
static int k = 0;
void pailie(char *s,int start,int end)
{
if (start == end)
{
cout << s << endl;
return;
}
else
{
for (int i = start + 1; i <= end ; i++)
{
char temp = s[start];
s[start] = s[i];
s[i] = temp;
pailie(s,start + 1,end);
temp = s[start];
s[start] = s[i];
s[i] = temp;
pailie(s,start + 1,end);
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
pailie(s,0,2);
return 0;
}