枚举--排列/组合

#include<cstdio>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<math.h>
#define  ll long long 
using namespace std;
const int maxn=1000;
int A[maxn]; 
bool vis[maxn];
// 不可重复排列  
int ans=0;
void pailie(int i,int n){
	if(i==n+1){
		for(int j=1;j<=n;j++)
			printf("%d ",A[j]);
		putchar(10);
		ans++;
		return ;
	}
	for(int j=1;j<=n;j++){
		if(!vis[j]){
			vis[j]=1; 
			A[i]=j;
			pailie(i+1,n);
			vis[j]=0;
		}
	}
} 
int main(){
	pailie(1,3);
	printf("%d\n",ans);
	return 0;
}
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
6
#include<cstdio>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<math.h>
#define  ll long long 
using namespace std;
const int maxn=1000;
int A[maxn]; 
//可重复排列  
int ans=0;
void pailie(int i,int n){
	if(i==n+1){
		for(int j=1;j<=n;j++)
			printf("%d ",A[j]);
		putchar(10);
		ans++;
		return ;
	}
	for(int j=1;j<=n;j++){
			A[i]=j;
			pailie(i+1,n);
		
	}
} 
int main(){
	pailie(1,3);
	printf("%d\n",ans);
	
	return 0;
}
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3
27


#include<cstdio>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<math.h>
#define  ll long long 
using namespace std;
const int maxn=1000;
int A[maxn];
int L[maxn]; 
bool vis[maxn];
// 数目限制排列 
int ans=0;
void pailie(int i,int n){
	if(i==n+1){
		for(int j=1;j<=n;j++)
			printf("%d ",A[j]);
		putchar(10);
		ans++;
		return ;
	}
	for(int j=1;j<=n;j++){
		if(L[j]){
			L[j]--;
			A[i]=j;
			pailie(i+1,n);
			L[j]++;
		}
	}
} 
int main(){

	L[1]=2;
	L[2]=1;
	L[3]=2;
	L[4]=6;
	L[5]=3;
	pailie(1,3);
	printf("%d\n",ans);
	
	return 0;
}
1 1 2
1 1 3
1 2 1
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 3
2 3 1
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 3
3 3 1
3 3 2
18


#include<cstdio>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<math.h>
#define  ll long long 
using namespace std;
const int maxn=1000;
int A[maxn]; 
//组合 从n个{1...n}中选m个 
int ans=0;
void Co(int m,int n,int k){
	if(k==m+1){
		for(int i=1;i<=m;i++)
			printf("%d ",A[i]);
		putchar(10);
		ans++;
		return ;
	}
	for(int i=A[k-1]+1;i<=n;i++){
		A[k]=i;
		Co(m,n,k+1);
	}
} 
int main(){
	Co(3,5,1);
	printf("%d\n",ans);
	return 0;
}
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5
10

"AABBBC" 从中取三个元素,除去相同方案。每个方案可以有多个元素。
#include <stdio.h>
#include <string.h>
#include<iostream>
using namespace std;
int data[]={2,3,1};
int x[3];
char y[]="ABC";
int n;
int ans=0;
void f(int k,int m){//k是剩下的位置 是当前位置 
//printf("%d %d\n",k,m);
	if(m==n&&k)return ;
	if(k==0){
		for(int i=0;i<n;i++){
			for(int j=0;j<x[i];j++){
				printf("%c",y[i]);
			}
		}
		putchar(10);
		ans++;
		return ;
	}
	for(int i=0;i<=min(data[m],k);i++){
		x[m]=i;
		f(k-i,m+1);
		x[m]=0;
	}
}
int main()
{
	n=3;
	f(3,0);	
	return 0;
}



Euler-up/down 排列(也称为 **交替排列**)是组合数学中的一个重要概念。它指的是满足以下模式的排列- **Up-down 排列(Euler 数 $ E_n $)**: $$ a_1 < a_2 > a_3 < a_4 > \cdots $$ - **Down-up 排列(有时记作 $ D_n $)**: $$ a_1 > a_2 < a_3 > a_4 < \cdots $$ 在很多文献中,这两类排列统称为 Euler-up/down 排列,它们的数量之和即为第 $ n $ 个欧拉数 $ E_n $。 --- ## ✅ 枚举所有 Euler-up/down 排列的 Python 实现 我们可以使用递归或回溯法来枚举所有满足“升跌交替”条件的排列。 ### 以下是一个完整的实现: ```python from typing import List def generate_alternating_permutations(n: int, pattern: str = 'updown') -> List[List[int]]: """ 枚举所有满足 up-down 或 down-up 模式的排列 Args: n (int): 元素数量 (1 ~ n) pattern (str): 'updown' 表示 a1 < a2 > a3 < a4 > ... 'downup' 表示 a1 > a2 < a3 > a4 < ... Returns: List[List[int]]: 所有符合条件的排列列表 """ result = [] def backtrack(path: List[int], direction: bool): """ 回溯搜索 Args: path: 当前构造的排列 direction: True 表示下一位应上升, False 表示下降 """ if len(path) == n: result.append(path[:]) return for i in range(1, n + 1): if i in path: continue if not path: # 第一个元素没有方向要求 backtrack([i], direction) else: last = path[-1] if direction and i > last: backtrack(path + [i], not direction) elif not direction and i < last: backtrack(path + [i], not direction) if pattern == 'updown': # 第二个位置需要大于第一个位置(先升) for first in range(1, n + 1): for second in range(1, n + 1): if first != second and second > first: backtrack([first, second], False) # 下降开始 elif pattern == 'downup': for first in range(1, n + 1): for second in range(1, n + 1): if first != second and second < first: backtrack([first, second], True) # 上升开始 else: raise ValueError("pattern 必须是 'updown' 或 'downup'") return result # 示例:生成所有长度为4的 up-down 排列 n = 4 updown_perms = generate_alternating_permutations(n, pattern='updown') print(f"Length {n} up-down permutations ({len(updown_perms)} total):") for p in updown_perms: print(p) ``` --- ## ✅ 输出结果(当 `n=4` 时) ``` Length 4 up-down permutations (5 total): [1, 3, 2, 4] [1, 4, 2, 3] [2, 3, 1, 4] [2, 4, 1, 3] [3, 4, 1, 2] ``` --- ## ✅ 解释代码逻辑 - 我们使用了 **回溯算法(Backtracking)** 来尝试所有可能的排列- 每次选择下一个数字时,根据当前的方向(上升或下降)进行判断: - 如果是上升阶段,必须选比上一个大的; - 如果是下降阶段,必须选比上一个小的。 - 初始两个元素的选择决定了整个排列的起始方向。 - 最终将所有合法排列收集到 `result` 中返回。 --- ## ✅ 应用场景 | 领域 | 应用 | |------|------| | **组合数学** | 研究排列的结构、对称性 | | **密码学** | 构造伪随机序列 | | **概率统计** | 分析排列波动性与随机性 | | **计算机图形学** | 模拟自然变化趋势 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值