输入:
3
1 2 3
输出:
1
1 2
1 2 3
1 3
2
2 3
3
Delphi代码:
program full_combination;
const
max_n = 10;
var
n: integer;
rcd, num: array[0..max_n] of integer;
procedure full_combination(index, p: integer);
var
i: integer;
begin
//每次都输出
if index > 0 then
begin
for i := 0 to index - 2 do
Write(rcd[i], ' ');
writeln(rcd[index - 1]);
end;
for i := p to n - 1 do
begin
rcd[index] := num[i];
full_combination(index + 1, i + 1);
end;
end;
var
i: integer;
begin
while not seekeof do
begin
readln(n);
for i := 0 to n - 1 do
Read(num[i]);
full_combination(0, 0);
end;
end.
C++代码:
#include <stdio.h>
#define MAX_N 10
int rcd[MAX_N], num[MAX_N];
int n;
void full_combination(int index, int p)
{
int i;
for (i=0; i<index; i++)
{
printf("%d", rcd[i]);
if (i<index-1)
{
printf(" ");
}
}
printf("\n");
for (i=p; i<n; i++)
{
rcd[index] = num[i];
full_combination(index+1, i+1);
}
}
int read_data()
{
int i;
if (scanf("%d", &n)==EOF)
{
return 0;
}
for (i=0; i<n; i++)
{
scanf("%d", &num[i]);
}
return 1;
}
void main()
{
while(read_data())
{
full_combination(0,0);
}
}