2016校赛预赛 Problem B

本文介绍了一种使用递归方法解决给定数字范围内所有可能序列的字典序排列问题,通过实现一系列函数,从数字1到n构建了所有可能的组合,并以清晰的代码示例进行了展示。

不会递归真的很懵逼~~~

描述:给定一个数n,0<n<10,求出由数字1~n的构成的所以字典序

例如:n==3

1

2

3

1 2

1 3

2 3

1 2 3


#include<stdio.h>
#include<string.h>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const ll maxn=20;
//ll dp[10][10],a[10][10];
int p,a[maxn];

void print()
{
	for(int i=1;i<=p;i++)
	{
		if(a[i])
		cout<<a[i]<<" ";
	}
	cout<<endl;
}
int check()
{
	int ans=0;
	for(int i=1;i<=p;i++)
	{
		if(a[i])
		ans++;
	}
	return ans;
}
void solve(int x,int y)   //x:值,y:个数 
{
	if(x>p||check()>=y)
	return ;
	a[x]=x;
	if(check()==y)
	print();
	else 
	solve(x+1,y);
	a[x]=0;
	solve(x+1,y);
}
int main()
{
	cin>>p;
	int t=1;
	while(t<=p)
	{
		solve(1,t);
		t++;
	}
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值