algorithm - 产生给定长度的序列
问题描述
我正在寻找 C++ 中的代码来生成这样的序列:
输入:
n = 4(序列中的最大元素),k = 3(序列的长度)
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
1 2 4
1 4 2
2 1 4
2 4 1
4 1 2
4 2 1
我浏览了互联网,但只能得到给定输入长度的增加序列。我正在弄清楚如何产生这样的序列!
解决方案
我认为这就像你正在寻找的东西
#include <iostream>
#include <algorithm>
using namespace std;
int n, k;
int main(){
cin>>n;
cin>>k;
int a[n];
for(int i = 0; i < n; i++)
a[i] = i + 1;
do{
for(int i = 0; i < k; i++)
cout<<a[i]<<" ";
cout<<endl;
}while(next_permutation(a, a + n));
return 0;
}
推荐阅读
- html - Sass 插值容器高度
- reactjs - React.memo 不是 16.13.0 中的函数
- reactjs - 如何从 props 中获取 React-Table 中的 API JSON 数据?
- java - Spark Scala 代码中的“线程“dispatcher-event-loop-0”java.lang.OutOfMemoryError:Java 堆空间中的异常”错误
- machine-learning - 在 epoch 结束时,CNN 训练损失有规律的峰值
- system-verilog - SystemVerilog:虚拟模块与虚拟接口
- r - 从 Gitlab CI 使用 R 发送电子邮件
- angular - httpClient 的 http 操作是否总是单值 Observables?他们能以某种方式发出一个以上的值吗?
- java - 倒数计时器,在 JLabel 中显示秒数时出错
- python-3.6 - 基数为 10 的 int() 的无效文字: b'\x1f\x8b\x08\x08\x80\xff\xa8R\x02\x03GoogleNews-vectors-negative300.bin\