刷题集合1

92. 递归实现指数型枚举

从 1∼n 这 n个整数中随机选取任意多个,输出所有可能的选择方案。

输入格式

输入一个整数 n。

输出格式

每行输出一种方案。

同一行内的数必须升序排列,相邻两个数用恰好 11 个空格隔开。

对于没有选任何数的方案,输出空行。

本题有自定义校验器(SPJ),各行(不同方案)之间的顺序任意。

数据范围

1≤n≤15


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N =15;
int n;

int st[N];
void dfs(int u){
    if(u>n){
        for(int i=1;i<=n;i++){
            if(st[i]==1)
            printf("%d",i);
        }
        printf("\n");
        return;
    }
    st[u]=2;
    dfs(u+1);
    st[u]=0;
    st[u]=1;
    dfs(u+1);
    st[u]=0;
}
int main(){
    cin>>n;
    dfs(1);
    return 0;
}

 93. 递归实现组合型枚举

从 1∼n1 这 n个整数中随机选出 m个,输出所有可能的选择方案。

输入格式

两个整数 n,m在同一行用空格隔开。

输出格式

按照从小到大的顺序输出所有方案,每行 11 个。

首先,同一行内的数升序排列,相邻两个数用一个空格隔开。

其次,对于两个不同的行,对应下标的数一一比较,字典序较小的排在前面(例如 1 3 5 7 排在 1 3 6 8 前面)。

数据范围

n>0 ,
0≤m≤n,
n+(n−m)≤25

输入样例:
5 3
输出样例:
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 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int n;
int m;
const int N=25;
int st[N];
int used[N];
void dfs(int u,int start){
    if(u>m){
        for(int i=1;i<=m;i++)
         {printf("%d ",st[i]);}
         puts("");
         return;
    }
    for(int i=start;i<=n;i++){
      
            st[u]=i;
        
            dfs(u+1,i+1);
       
            st[u]=0;
        
    }
        
    }
    
int main(){
    scanf("%d %d",&n,&m);
    dfs(1,1);
    return 0;
    

94. 递归实现排列型枚举

把 1∼n 这 n 个整数排成一行后随机打乱顺序,输出所有可能的次序。

输入格式

一个整数 n。

输出格式

按照从小到大的顺序输出所有方案,每行 11 个。

首先,同一行相邻两个数用一个空格隔开。

其次,对于两个不同的行,对应下标的数一一比较,字典序较小的排在前面。

数据范围

1≤n≤9

输入样例:
3
输出样例:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int n;
const int N=15;
int st[N];
bool used[N];
void dfs(int u){
    if(u>n){
        for(int i=1;i<=n;i++){
                printf("%d ",st[i]);
}
        puts("");
        return;
    }
    for(int i=1;i<=n;i++){
        if(!used[i]){
            st[u]=i;
            used[i]=true;
            dfs(u+1);
            st[u]=0;
            used[i]=false;
        }
    }
    
    
    
}
int main(){
    scanf("%d",&n);
    dfs(1);
    return 0;

    
}

4翻硬币 

 

题目背景

小明正在玩一个“翻硬币”的游戏。

题目描述

桌上放着排成一排的若干硬币。我们用 * 表示正面,用 o 表示反面(是小写字母,不是零),比如可能情形是 **oo***oooo,如果同时翻转左边的两个硬币,则变为 oooo***oooo。现在小明的问题是:如果已知了初始状态和要达到的目标状态,每次只能同时翻转相邻的两个硬币,那么对特定的局面,最少要翻动多少次呢?

输入格式

两行等长字符串,分别表示初始状态和要达到的目标状态,每行长度小于 10001000。

数据保证一定存在至少一种方案可以从初始状态和要达到的目标状态。

输出格式

一个整数,表示最小操作步数。

输入输出样例

输入 #1复制

**********
o****o****

输出 #1复制

5

输入 #2复制

*o**o***o***
*o***o**o***

输出 #2复制

1
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int n;
const int N=10;
string start;
string end1;
void turn(int i){
    if(start[i]=='o')start[i]='*';
    else{start[i]='o';}
}
int main(){
    cin>>start;
    cin>>end1;
    int res=0;
    int len1=start.length();
    for(int i=0;i<len1;i++){
        if(start[i]!=end1[i]){
        turn(i);
        turn(i+1);
        res++;
        }
    }
  
    cout<<res<<endl;
    return 0;
 
}

 5 带分数

题目描述

100100 可以表示为带分数的形式:100=3+69258714100=3+71469258​。

还可以表示为:100=82+3546197100=82+1973546​。

注意特征:带分数中,数字 11 ~ 99 分别出现且只出现一次(不包含 00)。

类似这样的带分数,100100 有 1111 种表示法。

输入格式

从标准输入读入一个正整数 �(�<106)N(N<106)。

输出格式

程序输出数字 �N 用数码 11 ~ 99 不重复不遗漏地组成带分数表示的全部种数。

注意:不要求输出每个表示,只统计有多少表示法!

输入输出样例

输入 #1复制

100

输出 #1复制

11

输入 #2复制

105

输出 #2复制

6

说明/提示

原题时限 3 秒, 64M。蓝桥杯 2013 年第四届省赛

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int n;
int st[10];
int way=0;
int back[10];
bool check(int a,int c){
    int b=n*c-a*c;
    if(!b||!a||!c){
        return 0;
    }
    memcpy(back,st,sizeof st);
    while(b){
       int x=b%10;
        b=b/10;
        if(!x ||back[x]) return false;
        back[x]=1;
    }
    for(int i=1;i<=9;i++){
        if(!back[i]){
            return 0;
        }
    }
    return 1;
}
void dfs_c(int u,int a,int c){
    if(u==n){
        return;
    }
    if(check(a,c)){
    way++;
    }
    for(int i=1;i<=9;i++){
        if(!st[i]){
            st[i]=1;
            dfs_c(u+1,a,c*10+i);
            st[i]=0;
        }
        
    }
    
}


void dfs_a(int u,int a){
    if(a>=n){
        return ;
    }
    dfs_c(u,a,0);
    for(int i=1;i<=9;i++){
        if(!st[i]){
            st[i]=1;
            dfs_a(u+1,a*10+i);
            st[i]=0;
        }
        }
    }
 

int main(){
    scanf("%d",&n);
    dfs_a(0,0);
  
    cout<<way<<endl;
    return 0;
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值