排列組合中的組合,因此每次挑選的時候只能比之前挑的數字還要大,用遞迴做Backtraking,詳細見底下code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <algorithm> | |
using namespace std; | |
int N, S[100], choosed[100], ans[100]; | |
void func(int len, int pos) | |
{ | |
if (len == 6){ | |
for (int i = 0; i < 5; ++i) | |
printf("%d ", ans[i]); | |
printf("%d\n", ans[5]); | |
return; | |
} | |
for (int i = pos; i < N; ++i) { | |
if (!choosed[i]) { | |
choosed[i] = 1; | |
ans[len] = S[i]; | |
func(len+1, i + 1); | |
choosed[i] = 0; | |
} | |
} | |
} | |
int main() | |
{ | |
int Case = 0; | |
while (scanf("%d", &N) && N) { | |
if (Case++) printf("\n"); | |
for (int i = 0; i < N; ++i){ | |
scanf("%d", &S[i]); | |
choosed[i] = 0; | |
} | |
func(0,0); | |
} | |
} |
沒有留言:
張貼留言