11第k个排列

11第k个排列

1 题目描述

/*
 * 给定参数n,从1到n有n个整数:1,2,3,4,... n ; 共有n!种排列
 * 按大小顺序升序列出所有排列情况,并意义标记
 * 当n=3 时,所有排列如下:
 * 123  132  213  231  312  321
 *
 * 给出n和k,返回第k个排列
 *
输入
3
3


输出
213

说明:
3的排列有123,132,213…,那么第三位置就是213

 *
 * */

// 回溯找排列

2 代码


#include "iostream"
#include "vector"
using namespace std;

void dfs(vector<bool> &fg, int n, string path, string &res, int &count) {
  if (path.size() == n) {
    count--;
    if (count == 0) {
      res = path;
    }
    return ;
  }

  for (int i = 0; i < fg.size(); i++) {
    if (!fg[i]) {
      fg[i] = true;
      dfs(fg, n, path+ to_string(i+1), res, count);
      fg[i] = false;

      if (count == 0) {
        return;
      }
    }
  }
}

int main() {
  int n;
  int id;
  cin >> n >> id;
//  cin >> id;

  vector<bool> fg(n);
  string res;
  int count = id;
  dfs(fg, n, "", res, count);
  if (!res.empty()) {
    cout << res << endl;
  }
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值