vb.net变成 数组组合情况输出问题
vb.net编写,想做一个从N个数中取出M个的组合,输出对应的所有组合元素。不是计算组合的情况有多少种,而是把所有的组合都输出来的。最后的仙人谢谢你,C#方面我不太懂,能...
vb.net编写,想做一个从N个数中取出M个的组合,输出对应的所有组合元素。不是计算组合的情况有多少种,而是把所有的组合都输出来的。
最后的仙人
谢谢你,C#方面我不太懂,能帮忙给个语句的备注吗,我想看懂你的语句意思,算法,这样我可以转为VB.NET来。
这个C#的代码是不是算出C(N,M)的数量,没有输出具体是哪些组合吗? 展开
最后的仙人
谢谢你,C#方面我不太懂,能帮忙给个语句的备注吗,我想看懂你的语句意思,算法,这样我可以转为VB.NET来。
这个C#的代码是不是算出C(N,M)的数量,没有输出具体是哪些组合吗? 展开
2个回答
展开全部
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication42
{
class Program
{
static void Main(string[] args)
{
count = 0;
String[] s = Console.ReadLine().Split(' ');
int[] array = new int[s.Length];
for (int i = 0; i < s.Length; i++)
{
array[i] = Convert.ToInt32(s[i]);
}
int m = Convert.ToInt32(Console.ReadLine());
List<int> arraylist = new List<int>();
arraylist.AddRange(array);
Output(arraylist, m, null);
Console.WriteLine("共有" + count.ToString() + "个");
Console.ReadKey();
}
static int count = 0;
static void Output(List<int> array, int m, List<int> output)
{
if (output == null)
output = new List<int>();
if (m == 0)
{
for (int i = 0; i < output.Count; i++)
{
Console.Write(output[i].ToString() + " ");
}
Console.Write("\r\n");
count++;
return;
}
else
{
for (int i = 0; i < array.Count; i++)
{
List<int> output2 = new List<int>();
output2.AddRange(output.ToArray());
output.Add(array[i]);
List<int> array2 = new List<int>();
array2.AddRange(array.ToArray());
for (int j = 0; j <= i; j++)
{
int number = array[j];
array2.Remove(number);
}
Output(array2, m - 1, output);
output.Clear();
output.AddRange(output2.ToArray());
}
}
}
}
}
额,没注意VB,用C#写的。。。
输出了的,你新建一个C#控制台程序,把这段贴进去,运行一下就知道了
先输入一组字符串,然后输入一个整数,就会把所有的组合输出出来
我把这段转成VB再发一次吧
Module Module1
Sub Main()
count = 0
Dim s As String() = Console.ReadLine().Split(" ")
Dim array(0 To s.Length - 1) As Integer
For i As Integer = 0 To s.Length - 1
array(i) = Convert.ToInt32(s(i))
Next
Dim m As Integer = Convert.ToInt32(Console.ReadLine())
Dim arraylist As New List(Of Integer)
arraylist.AddRange(array)
Print(arraylist, m, Nothing)
Console.WriteLine("共有" + count.ToString() + "个")
Console.ReadKey()
End Sub
Sub Print(ByRef array As List(Of Integer), ByVal m As Integer, ByRef output As List(Of Integer))
If output Is Nothing Then
output = New List(Of Integer)()
End If
If m = 0 Then
For i As Integer = 0 To output.Count - 1
Console.Write(output(i).ToString() + " ")
Next
Console.Write(vbCrLf)
count = count + 1
Return
Else
For i As Integer = 0 To array.Count - 1
Dim array2 As New List(Of Integer)
Dim output2 As New List(Of Integer)
output2.AddRange(output.ToArray())
output.Add(array(i))
array2.AddRange(array.ToArray())
For j As Integer = 0 To i
Dim number As Integer = array(j)
array2.Remove(number)
Next
Print(array2, m - 1, output)
output.Clear()
output.AddRange(output2.ToArray())
Next
End If
End Sub
Dim count As Integer = 0
End Module
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication42
{
class Program
{
static void Main(string[] args)
{
count = 0;
String[] s = Console.ReadLine().Split(' ');
int[] array = new int[s.Length];
for (int i = 0; i < s.Length; i++)
{
array[i] = Convert.ToInt32(s[i]);
}
int m = Convert.ToInt32(Console.ReadLine());
List<int> arraylist = new List<int>();
arraylist.AddRange(array);
Output(arraylist, m, null);
Console.WriteLine("共有" + count.ToString() + "个");
Console.ReadKey();
}
static int count = 0;
static void Output(List<int> array, int m, List<int> output)
{
if (output == null)
output = new List<int>();
if (m == 0)
{
for (int i = 0; i < output.Count; i++)
{
Console.Write(output[i].ToString() + " ");
}
Console.Write("\r\n");
count++;
return;
}
else
{
for (int i = 0; i < array.Count; i++)
{
List<int> output2 = new List<int>();
output2.AddRange(output.ToArray());
output.Add(array[i]);
List<int> array2 = new List<int>();
array2.AddRange(array.ToArray());
for (int j = 0; j <= i; j++)
{
int number = array[j];
array2.Remove(number);
}
Output(array2, m - 1, output);
output.Clear();
output.AddRange(output2.ToArray());
}
}
}
}
}
额,没注意VB,用C#写的。。。
输出了的,你新建一个C#控制台程序,把这段贴进去,运行一下就知道了
先输入一组字符串,然后输入一个整数,就会把所有的组合输出出来
我把这段转成VB再发一次吧
Module Module1
Sub Main()
count = 0
Dim s As String() = Console.ReadLine().Split(" ")
Dim array(0 To s.Length - 1) As Integer
For i As Integer = 0 To s.Length - 1
array(i) = Convert.ToInt32(s(i))
Next
Dim m As Integer = Convert.ToInt32(Console.ReadLine())
Dim arraylist As New List(Of Integer)
arraylist.AddRange(array)
Print(arraylist, m, Nothing)
Console.WriteLine("共有" + count.ToString() + "个")
Console.ReadKey()
End Sub
Sub Print(ByRef array As List(Of Integer), ByVal m As Integer, ByRef output As List(Of Integer))
If output Is Nothing Then
output = New List(Of Integer)()
End If
If m = 0 Then
For i As Integer = 0 To output.Count - 1
Console.Write(output(i).ToString() + " ")
Next
Console.Write(vbCrLf)
count = count + 1
Return
Else
For i As Integer = 0 To array.Count - 1
Dim array2 As New List(Of Integer)
Dim output2 As New List(Of Integer)
output2.AddRange(output.ToArray())
output.Add(array(i))
array2.AddRange(array.ToArray())
For j As Integer = 0 To i
Dim number As Integer = array(j)
array2.Remove(number)
Next
Print(array2, m - 1, output)
output.Clear()
output.AddRange(output2.ToArray())
Next
End If
End Sub
Dim count As Integer = 0
End Module
展开全部
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
ConsoleApplication42
{
class
Program
{
static
void
Main(string[]
args)
{
count
=
0;
String[]
s
=
Console.ReadLine().Split('
');
int[]
array
=
new
int[s.Length];
for
(int
i
=
0;
i
<
s.Length;
i++)
{
array[i]
=
Convert.ToInt32(s[i]);
}
int
m
=
Convert.ToInt32(Console.ReadLine());
List<int>
arraylist
=
new
List<int>();
arraylist.AddRange(array);
Output(arraylist,
m,
null);
Console.WriteLine("共有"
+
count.ToString()
+
"个");
Console.ReadKey();
}
static
int
count
=
0;
static
void
Output(List<int>
array,
int
m,
List<int>
output)
{
if
(output
==
null)
output
=
new
List<int>();
if
(m
==
0)
{
for
(int
i
=
0;
i
<
output.Count;
i++)
{
Console.Write(output[i].ToString()
+
"
");
}
Console.Write("\r\n");
count++;
return;
}
else
{
for
(int
i
=
0;
i
<
array.Count;
i++)
{
List<int>
output2
=
new
List<int>();
output2.AddRange(output.ToArray());
output.Add(array[i]);
List<int>
array2
=
new
List<int>();
array2.AddRange(array.ToArray());
for
(int
j
=
0;
j
<=
i;
j++)
{
int
number
=
array[j];
array2.Remove(number);
}
Output(array2,
m
-
1,
output);
output.Clear();
output.AddRange(output2.ToArray());
}
}
}
}
}
额,没注意VB,用C#写的。。。
输出了的,你新建一个C#控制台程序,把这段贴进去,运行一下就知道了
先输入一组字符串,然后输入一个整数,就会把所有的组合输出出来
我把这段转成VB再发一次吧
Module
Module1
Sub
Main()
count
=
0
Dim
s
As
String()
=
Console.ReadLine().Split("
")
Dim
array(0
To
s.Length
-
1)
As
Integer
For
i
As
Integer
=
0
To
s.Length
-
1
array(i)
=
Convert.ToInt32(s(i))
Next
Dim
m
As
Integer
=
Convert.ToInt32(Console.ReadLine())
Dim
arraylist
As
New
List(Of
Integer)
arraylist.AddRange(array)
Print(arraylist,
m,
Nothing)
Console.WriteLine("共有"
+
count.ToString()
+
"个")
Console.ReadKey()
End
Sub
Sub
Print(ByRef
array
As
List(Of
Integer),
ByVal
m
As
Integer,
ByRef
output
As
List(Of
Integer))
If
output
Is
Nothing
Then
output
=
New
List(Of
Integer)()
End
If
If
m
=
0
Then
For
i
As
Integer
=
0
To
output.Count
-
1
Console.Write(output(i).ToString()
+
"
")
Next
Console.Write(vbCrLf)
count
=
count
+
1
Return
Else
For
i
As
Integer
=
0
To
array.Count
-
1
Dim
array2
As
New
List(Of
Integer)
Dim
output2
As
New
List(Of
Integer)
output2.AddRange(output.ToArray())
output.Add(array(i))
array2.AddRange(array.ToArray())
For
j
As
Integer
=
0
To
i
Dim
number
As
Integer
=
array(j)
array2.Remove(number)
Next
Print(array2,
m
-
1,
output)
output.Clear()
output.AddRange(output2.ToArray())
Next
End
If
End
Sub
Dim
count
As
Integer
=
0
End
Module
System;
using
System.Collections.Generic;
using
System.Text;
namespace
ConsoleApplication42
{
class
Program
{
static
void
Main(string[]
args)
{
count
=
0;
String[]
s
=
Console.ReadLine().Split('
');
int[]
array
=
new
int[s.Length];
for
(int
i
=
0;
i
<
s.Length;
i++)
{
array[i]
=
Convert.ToInt32(s[i]);
}
int
m
=
Convert.ToInt32(Console.ReadLine());
List<int>
arraylist
=
new
List<int>();
arraylist.AddRange(array);
Output(arraylist,
m,
null);
Console.WriteLine("共有"
+
count.ToString()
+
"个");
Console.ReadKey();
}
static
int
count
=
0;
static
void
Output(List<int>
array,
int
m,
List<int>
output)
{
if
(output
==
null)
output
=
new
List<int>();
if
(m
==
0)
{
for
(int
i
=
0;
i
<
output.Count;
i++)
{
Console.Write(output[i].ToString()
+
"
");
}
Console.Write("\r\n");
count++;
return;
}
else
{
for
(int
i
=
0;
i
<
array.Count;
i++)
{
List<int>
output2
=
new
List<int>();
output2.AddRange(output.ToArray());
output.Add(array[i]);
List<int>
array2
=
new
List<int>();
array2.AddRange(array.ToArray());
for
(int
j
=
0;
j
<=
i;
j++)
{
int
number
=
array[j];
array2.Remove(number);
}
Output(array2,
m
-
1,
output);
output.Clear();
output.AddRange(output2.ToArray());
}
}
}
}
}
额,没注意VB,用C#写的。。。
输出了的,你新建一个C#控制台程序,把这段贴进去,运行一下就知道了
先输入一组字符串,然后输入一个整数,就会把所有的组合输出出来
我把这段转成VB再发一次吧
Module
Module1
Sub
Main()
count
=
0
Dim
s
As
String()
=
Console.ReadLine().Split("
")
Dim
array(0
To
s.Length
-
1)
As
Integer
For
i
As
Integer
=
0
To
s.Length
-
1
array(i)
=
Convert.ToInt32(s(i))
Next
Dim
m
As
Integer
=
Convert.ToInt32(Console.ReadLine())
Dim
arraylist
As
New
List(Of
Integer)
arraylist.AddRange(array)
Print(arraylist,
m,
Nothing)
Console.WriteLine("共有"
+
count.ToString()
+
"个")
Console.ReadKey()
End
Sub
Sub
Print(ByRef
array
As
List(Of
Integer),
ByVal
m
As
Integer,
ByRef
output
As
List(Of
Integer))
If
output
Is
Nothing
Then
output
=
New
List(Of
Integer)()
End
If
If
m
=
0
Then
For
i
As
Integer
=
0
To
output.Count
-
1
Console.Write(output(i).ToString()
+
"
")
Next
Console.Write(vbCrLf)
count
=
count
+
1
Return
Else
For
i
As
Integer
=
0
To
array.Count
-
1
Dim
array2
As
New
List(Of
Integer)
Dim
output2
As
New
List(Of
Integer)
output2.AddRange(output.ToArray())
output.Add(array(i))
array2.AddRange(array.ToArray())
For
j
As
Integer
=
0
To
i
Dim
number
As
Integer
=
array(j)
array2.Remove(number)
Next
Print(array2,
m
-
1,
output)
output.Clear()
output.AddRange(output2.ToArray())
Next
End
If
End
Sub
Dim
count
As
Integer
=
0
End
Module
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询