Talk is cheap. Show me the code. —— Linus Torvalds
循环版本
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
// freopen("i.txt", "r", stdin);// freopen("o.txt", "w", stdout);int s[50], k, ans[6];int a1, a2, a3, a4, a5, a6, br = 0;while(scanf("%d", &k), k){
for(int i = 0; i < k; i++) scanf("%d", &s[i]);if(br) putchar('\n');for(a1 = 0; a1 < k - 5; a1++){
ans[0] = s[a1];for(a2 = a1 + 1; a2 < k - 4; a2++){
ans[1] = s[a2];for(a3 = a2 + 1; a3 < k - 3; a3++){
ans[2] = s[a3];for(a4 = a3 + 1; a4 < k - 2; a4++){
ans[3] = s[a4];for(a5 = a4 + 1; a5 < k - 1; a5++){
ans[4] = s[a5];for(a6 = a5 + 1; a6 < k; a6++){
ans[5] = s[a6];for(int i = 0; i < 6; i++)printf("%d%c", ans[i], i == 5 ? '\n' : ' ');}}}}}}br = 1;}return 0;
}
递归版本
#include <iostream>
#include <cstdio>
using namespace std;
int s[50], k, ans[6];
void dfs(int n, int index)
{
if(n == 0){
for(int i = 0; i < 6; i++)printf("%d%c", ans[i], i == 5 ? '\n' : ' ');return ;}for(int i = index + 1; i < k - (n - 1); i++){
ans[6 - n] = s[i];dfs(n - 1, i);}
}
int main()
{
// freopen("i.txt", "r", stdin);// freopen("o.txt", "w", stdout);int br = 0;while(scanf("%d", &k), k){
for(int i = 0; i < k; i++) scanf("%d", &s[i]);if(br) putchar('\n');dfs(6, -1);br = 1;}return 0;
}