L1-6 求符合给定条件的整数集

题目描述

给定不超过6的正整数A,考虑从A开始的连续4个数字。请输出所有由它们组成的无重复数字的3位数。

输入

输入在一行中给出A。

输出

输出满足条件的的3位数,要求从小到大,每行6个整数。整数间以空格分隔,但行末不能有多余空格。

样例输入

2

样例输出

234 235 243 245 253 254
324 325 342 345 352 354
423 425 432 435 452 453
523 524 532 534 542 543

思路

两个思路
1.用next_permutation枚举存入set后再输出
2.类似dfs

思路一 代码

#include <bits/stdc++.h>
using namespace std;

set<int> st;
int a[3];

int main(){
    int n;
    cin>>n;
    a[0] = n;
    a[1] = n+1;
    a[2] = n+2;
    do{
        st.insert(a[0]*100+a[1]*10+a[2]); 
    }while(next_permutation(a, a+3));

    a[0] = n;
    a[1] = n+1;
    a[2] = n+3;
    do{
        st.insert(a[0]*100+a[1]*10+a[2]); 
    }while(next_permutation(a, a+3));

    a[0] = n;
    a[1] = n+2;
    a[2] = n+3;
    do{
        st.insert(a[0]*100+a[1]*10+a[2]); 
    }while(next_permutation(a, a+3));

    a[0] = n+1;
    a[1] = n+2;
    a[2] = n+3;
    do{
        st.insert(a[0]*100+a[1]*10+a[2]); 
    }while(next_permutation(a, a+3));

    int cnt = 0;
    for(set<int>::iterator it=st.begin(); it!=st.end(); it++){
        if(cnt){
            cout<<' ';
        }
        cout<<*it;
        if(++cnt%6 == 0){
            cnt = 0;
            cout<<endl;
        }

    }

    return 0;
} 

思路二 代码

#include <bits/stdc++.h>
using namespace std;

int book[10];

int main(){
    int a;
    int cnt = 0;
    cin>>a;
    int i, j, k;
    for(i=a; i<a+4; i++){
        book[i] = 1;
        for(j=a; j<a+4; j++){
            if(book[j] == 0){
                book[j] = 1;
                for(k=a; k<a+4; k++){
                    if(book[k] == 0){
                        book[k] = 1;
                        if(cnt){
                            cout<<' ';
                        }
                        cout<<i<<j<<k;
                        if(++cnt==6){
                            cnt = 0;
                            cout<<endl;
                        }
                        book[k] = 0;
                    }
                }
                book[j] = 0;
            }
        }
        book[i] = 0;
    }


    return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值