鱼C论坛

 找回密码
 立即注册
查看: 646|回复: 1

[已解决]C++

[复制链接]
发表于 2023-11-22 08:09:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
描述
错排问题:编号是1、2、…、n的n封信,装入编号为1、2、…、n的n个信封,要求每封信和信封的编号不同,问有多少种装法。

走楼梯时,每次能跨一级、二级、……、n级,但I级能且仅能跨一次,且要求第i次跨步的大小不能是i级楼梯。n*(n+1)/2级楼梯有几种走法,输出所有的走法

输入
一个正整数n(n<10)
输出
多行,每行表示一种走楼梯的方法
样例输入
3
样例输出
231
312
最佳答案
2023-11-22 08:09:18
这是一个经典的错排问题和楼梯走法问题。下面是C++的解决方案:

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;

  5. // 错排问题
  6. int derangement(int n) {
  7.     if (n == 1) return 0;
  8.     if (n == 2) return 1;
  9.     return (n - 1) * (derangement(n - 1) + derangement(n - 2));
  10. }

  11. // 楼梯走法问题
  12. void dfs(vector<int>& path, vector<vector<int>>& result, int n) {
  13.     if (path.size() == n) {
  14.         result.push_back(path);
  15.         return;
  16.     }
  17.     for (int i = 1; i <= n; ++i) {
  18.         if (find(path.begin(), path.end(), i) == path.end()) {
  19.             if (path.empty() || abs(i - path.back()) != path.size() + 1) {
  20.                 path.push_back(i);
  21.                 dfs(path, result, n);
  22.                 path.pop_back();
  23.             }
  24.         }
  25.     }
  26. }

  27. int main() {
  28.     int n;
  29.     cin >> n;

  30.     // 错排问题
  31.     cout << derangement(n) << endl;

  32.     // 楼梯走法问题
  33.     vector<vector<int>> result;
  34.     vector<int> path;
  35.     dfs(path, result, n);
  36.     for (const auto& r : result) {
  37.         for (int i : r) {
  38.             cout << i;
  39.         }
  40.         cout << endl;
  41.     }
  42.     return 0;
  43. }
复制代码


这段代码首先计算了错排问题的解,然后使用深度优先搜索(DFS)计算了楼梯走法问题的解。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-22 08:09:18 | 显示全部楼层    本楼为最佳答案   
这是一个经典的错排问题和楼梯走法问题。下面是C++的解决方案:

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;

  5. // 错排问题
  6. int derangement(int n) {
  7.     if (n == 1) return 0;
  8.     if (n == 2) return 1;
  9.     return (n - 1) * (derangement(n - 1) + derangement(n - 2));
  10. }

  11. // 楼梯走法问题
  12. void dfs(vector<int>& path, vector<vector<int>>& result, int n) {
  13.     if (path.size() == n) {
  14.         result.push_back(path);
  15.         return;
  16.     }
  17.     for (int i = 1; i <= n; ++i) {
  18.         if (find(path.begin(), path.end(), i) == path.end()) {
  19.             if (path.empty() || abs(i - path.back()) != path.size() + 1) {
  20.                 path.push_back(i);
  21.                 dfs(path, result, n);
  22.                 path.pop_back();
  23.             }
  24.         }
  25.     }
  26. }

  27. int main() {
  28.     int n;
  29.     cin >> n;

  30.     // 错排问题
  31.     cout << derangement(n) << endl;

  32.     // 楼梯走法问题
  33.     vector<vector<int>> result;
  34.     vector<int> path;
  35.     dfs(path, result, n);
  36.     for (const auto& r : result) {
  37.         for (int i : r) {
  38.             cout << i;
  39.         }
  40.         cout << endl;
  41.     }
  42.     return 0;
  43. }
复制代码


这段代码首先计算了错排问题的解,然后使用深度优先搜索(DFS)计算了楼梯走法问题的解。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-18 08:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表