69,371
社区成员
发帖
与我相关
我的任务
分享
'我没有C++环境了...用vbs凑合一下
'生成所有排列
Option Explicit
Dim result, counter
const N = 4
const Format = 10
Sub Swap(byRef InArray, first, second)
Dim t
t = InArray(first)
InArray(first) = InArray(second)
InArray(second) = t
End Sub
Function GenPer(byRef counter, N, Format)
Dim c(10), o(10) ,t(10)
Dim I, j, s, q, result
Dim oLoopFlag, iLoopFlag
For I = 0 To N - 1
c(I) = 0
o(I) = 1
t(I) = I
Next
oLoopFlag = True
counter = 0
While oLoopFlag
For I = 0 To N - 1
result = result & t(I)
Next
result = result & " "
counter = counter + 1
If counter Mod Format = 0 Then
result = result & chr(13) & chr(10)
End If
j = N - 1
s = 0
iLoopFlag = True
While iLoopFlag
q = c(j) + o(j)
If q > j or q < 0 Then
If j = 0 Then
iLoopFlag = False
oLoopFlag = False
Else
If q > j Then
s = s + 1
End If
o(j) = -o(j)
j = j - 1
End If
Else
Swap t, j - c(j) + s, j - q + s
c(j) = q
iLoopFlag = False
End If
Wend
Wend
Genper = result
End Function
result = GenPer(counter, N, Format)
result = result & chr(13) & chr(10) & "总数:" & counter
MsgBox(result)
//全排列函数
void proPerm(char *list,int i,int n)
{
int j,temp;
if(i==n)
{
for(j=0;j<=n;j++)
printf("%c",list[j]);
printf("%\n");
}
else
{
for(j=i;j<=n;j++)
{
SWAP(list[i],list[j],temp);
cout<<i<<j<<endl;
proPerm(list,i+1,n);
SWAP(list[i],list[j],temp);
}
}
}
#include <stdio.h>
#define MAX 10
int used[MAX];
int result[MAX];
int N;
void print()
{
int i;
for(i = 0; i < N; i++)
printf("%d ", result[i]);
printf("\n");
}
void proc(int step)
{
int i;
if(step == N)
print();
else
{
for(i = 0; i < N; i++)
{
if(!used[i])
{
used[i] = 1;
result[step] = i + 1;
proc(step + 1);
used[i] = 0;
}
}
}
}
int main()
{
scanf("%d", &N);
proc(0);
}