2-5: 有重复元素的排列问题:
参考代码:
#include <iostream>
#include <bits/stdc++.h>
#include <queue>
#include <stack>
#include <string>
using namespace std;
//有重复问题的排列问题
int n;
char a[101];
void Swap(int x,int y)
{
char t;
t=a[x];
a[x] = a[y];
a[y] = t;
}
int issame(int x,int y)
{
for(int k=x; k<y; k++)
{
if(a[k] == a[y])
return 1;
}
return 0;
}
void show()
{
for(int i=1; i<=n; i++)
{
cout<<a[i];
}
cout<<endl;
}
void permutation(int i)
{
if(i == n)
{
show();
return ;
}
for(int j=i; j<=n; j++)
{
if(issame(i,j))
{
continue;
}
Swap(j,i);
permutation(i+1);
Swap(j,i);
}
}
int main()
{
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>a[i];
}
permutation(1);
return 0;
}
2-6:排列的字典序问题:
参考代码:
#include <iostream>
#include <bits/stdc++.h>
#include <queue>
#include <stack>
#include <string>
using namespace std;
//排列的字典序问题
int n;
char a[101];
int f[50];
int factorial(int m)
{
if(m == 1)
{
f[m] = 1;
return f[m];
}
f[m] = m * factorial(m-1);
return f[m];
}
int calculate()
{
int sum = 0;
for(int i=1; i<=n; i++)
{
int coun = 0;
int x = a[i] - '0';
for(int j=1; j<=i; j++)
{
if(a[j]<=a[i])
{
coun++;
}
}
sum+=(x-coun) *f[n-i];
}
return sum;
}
void Swap(int x,int y)
{
char t;
t = a[x];
a[x] = a[y];
a[y] = t;
}
void get_next()//获取字典序排列的下一种排列
{
for(int i=n; i>=1; i--)
{
if(a[i]<a[n])
{
Swap(i,n);
break;
}
}
}
void show()
{
for(int i=1; i<=n; i++)
{
cout<<a[i];
}
cout<<endl;
}
int main()
{
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>a[i];
}
int l = factorial(n);
cout<<calculate()<<endl; //计算该排列的字典序值
get_next();
show();
return 0;
}
2-7:集合划分问题:
参考代码:
测试数据1
输入:
5
输出:
52
#include <iostream>
#include <bits/stdc++.h>
#include <queue>
#include <stack>
#include <string>
using namespace std;
// 集合划分问题
/*
采用分治算法进行求解问题:
1 假设存在元素a[n],并且对于前n-1个元素组成的集合而言,只有两种情况。
2 情况一: 元素a[n]若是插入到任意一个已存在的子集中,则set_part(n,m) = m*set_part(n-1,m);
3 情况二: 元素a[n]若是单独成为一个子集,则set_part(n-1,m-1);
*/
int sum;
int set_part(int n,int m)
{
if(m == 1)
return 1;
if(n == m)
return 1;
return set_part(n-1,m-1)+m*set_part(n-1,m);
}
int main()
{
int n;
cin>>n;
sum = 0;
for(int i=1; i<=n; i++)
{
sum+=set_part(n,i);
}
cout<<sum<<endl;
return 0;
}
2-8 集合划分问题2
参考代码:
#include <iostream>
#include <bits/stdc++.h>
#include <queue>
#include <stack>
#include <string>
using namespace std;
// 集合划分问题2
/*
采用分治算法进行求解问题:
1 假设存在元素a[n],并且对于前n-1个元素组成的集合而言,只有两种情况。
2 情况一: 元素a[n]若是插入到任意一个已存在的子集中,则set_part(n,m) = m*set_part(n-1,m);
3 情况二: 元素a[n]若是单独成为一个子集,则set_part(n-1,m-1);
*/
int sum;
int set_part(int n,int m)
{
if(m == 1)
return 1;
if(n == m)
return 1;
return set_part(n-1,m-1)+m*set_part(n-1,m);
}
int main()
{
int n,m;
cin>>n>>m;
sum = 0;
cout<<set_part(n,m)<<endl;
return 0;
}
2-9 双色Hanoi塔问题
参考代码:
#include <iostream>
#include <bits/stdc++.h>
#include <queue>
#include <stack>
#include <string>
using namespace std;
// 双色Hanoi塔问题
void move(int n, char a, char c, char b)
{
if(n == 1)
{
cout<<n<<" "<<a<<" "<<b<<endl;
return ;
}
move(n-1,a,b,c);
cout<<n<<" "<<a<<" "<<b<<endl;
move(n-1,c,a,b);
}
int main()
{
int n;
cin>>n;
move(n,'A','C','B');
return 0;
}
2-10:标准二维表问题:
解题思路: 利用进栈出栈相关
参考代码:
#include <iostream>
#include <bits/stdc++.h>
#include <queue>
#include <stack>
#include <string>
using namespace std;
// 标准二维表问题
long long a[1000];
void get_()
{
a[0] = 1;
for(int i=1;i<1001;i++)
{
long long sum = 0;
for(int j=0; j<i; j++)
{
sum+=a[j]*a[i-j-1];
sum = sum%1000000007;
}
a[i] = sum;
}
}
int main()
{
int n;
cin>>n;
get_();
cout<<a[n]<<endl;
return 0;
}
书上例题
2-11 循环赛日程表
示例1
输入:
8
输出:
1 2 3 4 5 6 7 8
2 1 4 3 6 5 8 7
3 4 1 2 7 8 5 6
4 3 2 1 8 7 6 5
5 6 7 8 1 2 3 4
6 5 8 7 2 1 4 3
7 8 5 6 3 4 1 2
8 7 6 5 4 3 2 1
参考代码:
#include <iostream>
#include <bits/stdc++.h>
#include <queue>
#include <stack>
#include <string>
using namespace std;
// 循环赛日程表
int a[101][101];
void init_(int n)
{
for(int i=1; i<=n; i++)
{
a[i][1] = i;
}
}
void set_part(int n,int l)
{
if(l == 1)
return ;
int nid = l/2;
set_part(n,nid);
int op = n/l;
for(int k =0 ;k<op; k++)
{
for(int i=1; i<=nid; i++)
{
for(int j=nid+1;j<=l;j++)
{
a[i+k*l][j] = a[i+k*l+nid][j-nid];
}
}
for(int i=nid+1; i<=l; i++)
{
for(int j=nid+1; j<=l;j++)
{
a[i+k*l][j] = a[i+k*l-nid][j-nid];
}
}
}
}
void show(int n)
{
for(int i=1; i<=n; i++)
{
for(int j=1;j<=n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
int main()
{
int n;
cin>>n;
init_(n);
set_part(n,n);
show(n);
return 0;
}