各位大神,請教個問題。
動態陣列中的每個元素(元素個數N不定,最多10個),兩兩組合、三三組合、NN組合。。。。。。,然后再排列。就是所有組合的所有排列。
例如:陣列a(10)中有東、西、南、北、中、發、白。。。。。。
兩兩組合成:東西、西東、東南、南東。。。。。。
三三組合:東西南、東南西、西南東、西東南、南東西、南西東
中發白、中白發、發白中、發中白、白發中、白中發
。。。。。。
四四組合:。。。。。。
NN組合:。。。。。。
然后生成的組合存盤在一個txt檔案中。
uj5u.com熱心網友回復:
僅供參考,盡管是C語言://qplw.cpp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int v=0;
int w=0;
int m;//記錄字串長度
int n;//記錄字串中的字符種類數
char map[256];//記錄是哪幾種字符
int count[256];//記錄每種字符有多少個
int stack[1000];//遞回用的堆疊,并記錄當前生成的排列
void Make_Map(char *str) {//統計字串的相關資訊
int s[256];
int i;
memset(s,0,sizeof(s));
memset(count,0,sizeof(count));
m=strlen(str);
if (w<1 || m<w) w=m;
while(*str) {
s[*str]++;
str++;
}
n=0;
for (i=0;i<256;i++)
if (s[i]) {
map[n]=i;
count[n]=s[i];
n++;
}
}
void Find(int depth) {//遞回式回溯法生成全排列
if (depth==w) {
int i;
for (i=0;i<depth;i++) putchar(map[stack[i]]);
putchar('\n');
} else {
int i;
if (v && depth>0) {
for (i=0;i<depth;i++) putchar(map[stack[i]]);
putchar('\n');
}
for (i=0;i<n;i++)
if (count[i]) {
stack[depth]=i;
count[i]--;
Find(depth+1);
count[i]++;
}
}
}
void main(int argc,char**argv) {
if (argc<2) {
printf("%s 要產生全排列的字串 [限定長度|-1]\n",argv[0]);
return;
}
if (argc>=3) w=atoi(argv[2]);
if (-1==w) v=1;
Make_Map(argv[1]);
Find(0);
}
//C:\test>qplw
//qplw 要產生全排列的字串 [限定長度|-1]
//
//C:\test>qplw 123
//123
//132
//213
//231
//312
//321
//
//C:\test>qplw 123 2
//12
//13
//21
//23
//31
//32
//
//C:\test>qplw 122333 3
//122
//123
//132
//133
//212
//213
//221
//223
//231
//232
//233
//312
//313
//321
//322
//323
//331
//332
//333
//
//C:\test>qplw 123 -1
//1
//12
//123
//13
//132
//2
//21
//213
//23
//231
//3
//31
//312
//32
//321
//
uj5u.com熱心網友回復:
看起來好像是個麻將游戲哦uj5u.com熱心網友回復:
遞回即可Dim t
Private Sub Command1_Click()
Dim arr() As String, i As Long
t = Split("東、西、南、北、中、發、白", "、")
Open "d:\MJ.TXT" For Output As #1
For i = 2 To UBound(t) + 1
getall UBound(t), i, "", arr
Print #1, Join(arr, ",") & vbCrLf
Next
Close #1
MsgBox "OK"
End Sub
Sub getall(ByVal m As Byte, ByVal n As Byte, ByRef a As String, ByRef arr() As String, Optional ByRef count As Long)
If Len(a) = n Then
count = count + 1
ReDim Preserve arr(1 To count)
arr(count) = a
Exit Sub
End If
For i = 0 To m
If InStr(a, t(i)) = 0 Then getall m, n, a & t(i), arr, count
Next i
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/118107.html
標籤:VB基礎類
上一篇:求大神幫忙編個小程式!小弟在這先謝謝了(某三角形問題)
下一篇:請問vb6如何反射dll檔案?
