全排序
#include<iostream>
#include<cstring>
using namespace std;
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
void permutationString(char s[],int start){
int len=strlen(s);
if(start==len)
{
for(inti=0;i<len;i++)
cout<<s[i]<<" ";
cout<<endl;
}
for(int i=start;i<len;i++)
{
swap(s[start],s[i]);
permutationString(s,start+1);
swap(s[start],s[i]);
}
}
void permutation(char s[])
{
if(s==NULL)
return;
permutationString(s,0);
}
int main()
{
char s[]="abc";
permutation(s);
}
字符串组合的问题
#include<iostream>
#include<vector>
using namespace std;
int num=0;
void combination(char s[],int start,int m,int len,vector<char>&result)
{
if(s==NULL||len<0)
return;
if(m==0)
{
num++;
for(inti=0;i<result.size();i++)
{
cout<<result[i];
}
cout<<endl;
return;
}
if(start==len)
return;
result.push_back(s[start]); //加入
combination(s,start+1,m-1,len,result);
result.pop_back();
combination(s,start+1,m,len,result);
}
int main()
{
char s[]="abc";
int len=strlen(s);
vector<char> result;
for(int i=1;i<=len;i++)
{
combination(s,0,i,len,result);
}
cout<<num<<endl;
}
两个字符串S1,S2,将s1插入S2的某个位置,判断插入后的s2是否是回文。
bool huiwen(char s[]) {
int len = strlen(s);
bool flag = true;
for (int i = 0, j = len - 1; i < j; i++, j--)
if (s[i] != s[j])
flag = false;
return flag;
}
int main() {
char s1[100];
char s2[100];
char temp[200];
memset(temp,0,sizeof(temp));
cin >> s1 >> s2;
int len1 = strlen(s1);
int len2 = strlen(s2);
bool totalflag=false;
for (int i = 0; i <= len2; i++) {
int k = i;
strcpy(temp, s2);
strcpy(temp + i, s1);
//cout<< temp << endl;
for (int j = i + len1; j< (len1 + len2); j++)
temp[j] =s2[k++];
cout << temp << endl;
bool flag =huiwen(temp);
if(flag==true)
totalflag=true;
}
cout<<totalflag<<endl;
return 0;
}
求一个整数的组合,如5=1+1+1+1+1=1+2+2=2+3=。。。。=1+4
#include<iostream>
#include<vector>
using namespace std;
vector<int> result;
int countnum= 0;
void getPartition(int a[], int start, int left, int len) {
if (left == 0) {
countnum++;
for (int i = 0; i <result.size(); i++) {
cout <<result[i] << " ";
//result.pop_back(); //清空
}
cout << endl;
return;
}
for (int i = start; i < len; i++) {
int select = a[i];
int y = left;
int cnt = 0;
while (y >=select) {
result.push_back(select);
y -= select;
getPartition(a,i + 1, y, len);
cnt++;
}
while (cnt--) {
result.pop_back();
}
}
}
int main() {
int n;
while (cin >> n && n) {
int a[n];
int num[n];
for (int i = 0; i < n-1;i++) {
a[i] = i + 1;
}
getPartition(a, 0, n, n-1);
cout << countnum<<endl;
}
}
求数组长度
首先一定要记住一点:
template <class Type,int size>
Type min(const Type (&Array)[size]){//size用来接收数组的长度--元素个数
Type min_value = Array[0];
for(int i=1;i<size;++i){
if(Array[i]<min_value) min_value=Array[i];
}
return min_value;
}
有了以上的函数模板,然后就可以使用min这个函数了:
int main()
{
float fa[5]={1.1,2.2,3.3,4.4,5.5};
int ia[]={1,2,3,4,5,6,7,8,9,0};
float fmin=min(fa);//(1)
int imin=min(ia);//(2)
return 0;
}
在(1)处,函数min()就根据模板变成了下面的样子:
float min(const float (&Array)[5]){
float min_value=Array[0];
for(int i=1;i<5;++i)
if(Array[i]<min_value) min_value=Array[i];
return min_value;
}
在(2)处,函数min()就根据模板变成了下面的样子:
int min(const int (&Array)[10]){
int min_value=Array[0];
for(int i=1;i<10;++i)
if(Array[i]<min_value) min_value=Array[i];
return min_value;
}
用位运算实现加法:
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int add(int num1,int num2)
{
int sum=num1^num2;
int carry=(num1&num2)<<1;
num1=sum;
num2=carry;
while(carry!=0)
{
sum=num1^num2;
carry=(num1&num2)<<1;
num1=sum;
num2=carry;
}
return num1;
}
int main() {
cout<<add(3,8)<<endl;
cout<<add(-3,8)<<endl;
return 0;
}
计算 序列的全排序的代码,用递归
#include<iostream>
using namespace std;
int num=0; //the number of list
void printArr(int str[],int len)
{
for(int i=0;i<len;i++)
cout<<str[i]<<" ";
cout<<endl;
}
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
void permutation(int str[],int start,int len)
{
if(str==NULL||len<=0)
return ;
if(start==len)
{
printArr(str,len);
num++;
}else
{
for(intj=start;j<len;j++)
{
swap(str[start],str[j]);
permutation(str,start+1,len);
swap(str[start],str[j]);
}
}
}
int main()
{
int str[]={1,2,3,4};
permutation(str,0,4);
cout<<num<<endl;
}
void Permutation(char* pStr, char* pBegin);
/
// Get the permutation of a string,
// for example, input string abc, its permutation is
// abc acb bac bca cba cab
/
void Permutation(char* pStr)
{
Permutation(pStr,pStr);
}
/
// Print the permutation of a string,
// Input: pStr -input string
// pBegin- points to the begin char of string
// which we want to permutate in thisrecursion
/
void Permutation(char* pStr, char* pBegin)
{
if(!pStr || !pBegin)
return;
// if pBegin pointsto the end of string,
// this round of permutation isfinished,
// print the permuted string
if(*pBegin == '\0')
{
printf("%s\n", pStr);
}
// otherwise,permute string
else
{
for(char* pCh = pBegin; *pCh != '\0'; ++ pCh)
{
// swap pCh and pBegin
char temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
Permutation(pStr, pBegin + 1);
// restore pCh and pBegin
temp = *pCh;
*pCh = *pBegin;
*pBegin = temp;
}
}
}