2016校赛预赛 Problem B
2016-03-26 15:18
281 查看
不会递归真的很懵逼~~~
描述:给定一个数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;
}
描述:给定一个数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;
}
相关文章推荐
- 有关数据库SQL递归查询在不同数据库中的实现方法
- C#中的递归APS和CPS模式详解
- WinForm实现按名称递归查找控件的方法
- 使用SqlServer CTE递归查询处理树、图和层次结构
- C#中的尾递归与Continuation详解
- C#递归实现显示文件夹及所有文件并计算其大小的方法
- php递归创建目录的方法
- PHP递归创建多级目录
- Javascript递归打印Document层次关系实例分析
- oracle 使用递归的性能提示测试对比
- 使用curl递归下载软件脚本分享
- Perl脚本实现递归遍历目录下的文件
- JavaScript的递归之递归与循环示例介绍
- C# 递归查找树状目录实现方法
- 全排列算法的非递归实现与递归实现的方法(C++)
- php递归列出所有文件和目录的代码
- java递归菜单树转换成pojo对象
- 一个JavaScript递归实现反转数组字符串的实例
- Java中的递归详解(用递归实现99乘法表来讲解)
- C语言的递归思想实例分析