weixin_44702271
2021-03-23 12:27
采纳率: 100%
浏览 102

VB.net如何将一个一维数组根据排列组合转化为二维数组

假设数组arr1(x)={1,2,3,2}

如何由数组arr1得到一个二维数组arr2

arr2(y,x)

{

0,0,0,0

0,0,0,1

0,0,0,2

0,0,1,0

0,0,1,1

0,0,1,2

0,0,2,0

.....

}

即二维数组每一行的值为一维数组每个值从0开始的排列组合,一维数组长度不一定是4,可能会更长。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 天际的海浪 2021-03-23 21:14
    已采纳
    Module Module1
    
    	Sub Main()
    		Dim arr1() As Integer = {1,3,1,2}
    		Dim snm, v, i, j, t As Integer
    		snm = 1
    		For Each v in arr1
    			snm = snm * (v+1)
    		Next
    		Dim arr2(snm-1,arr1.Length-1) As Integer
    		For i = 0 To snm-1
    			t = 1
    			For j = arr1.Length-1 To 0 step -1
    				arr2(i,j) = (i\t) MOD (arr1(j)+1)
    				t = t * (arr1(j)+1)
    		 	Next
    		Next
    
    		For i = 0 To snm-1
    			For j = 0 To arr1.Length-1
    				Console.Write(arr2(i,j)&" ")
    		 	Next
    			Console.WriteLine()
    		Next
    	End Sub
    
    End Module
    

    点赞 评论
  • 天际的海浪 2021-03-23 18:46
    arr1() = {1,3,1,2}
    snm = 1
    
    For Each v in arr1
    	snm = snm * (v+1)
    Next
    Dim arr2(arr1.Length,snm)
    For i = 0 To snm-1
    	t = 1
    	For j = arr1.Length-1 To 0 step -1
    		arr2(j,i) = (i\t) MOD (arr1(j)+1)
    		t = t * (arr1(j)+1)
     	Next
    Next
    
    点赞 评论

相关推荐 更多相似问题