Run ID 作者 问题 语言 测评结果 时间 内存 代码长度 提交时间
91666 sh25_shenpy 组合的输出 C++ 通过 1 MS 244 KB 844 2026-06-14 14:23:22

Tests(1/1):


#include <iostream> #include <vector> #include <iomanip> // 用于 setw 控制输出宽度 using namespace std; int n, r; vector<int> res; // 存储当前选中的组合 // 递归函数:start 起始数字,cnt 已选个数 void dfs(int start, int cnt) { // 选够 r 个数,输出组合 if (cnt == r) { for (int num : res) { cout << setw(3) << num; // 每个数字占3个字符 } cout << endl; return; } // 从 start 开始枚举,保证升序 for (int i = start; i <= n; ++i) { res.push_back(i); dfs(i + 1, cnt + 1); // 下一个数必须比当前大 res.pop_back(); // 回溯 } } int main() { cin >> n >> r; dfs(1, 0); // 从数字1开始,初始选0个 return 0; }


测评信息: