using System; using System.Collections.Generic; using System.Text;namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int[] array ={ 2,3,5,8,9,7,0,34,29,11}; int n=4; Method(array,n); } public static void Method(int[] arr, int N) { int[] prt = new int[100]; for (int i = 0; i < prt.Length; i++) { prt[i] = -9999; } for (int i = 0; i < arr.Length; i++) { int prti = 0; int k; for (k = i; k < N + i - 1; k++, prti++) { prt[prti] = arr[k]; } for (int j = k; j < arr.Length; j++) { prt[prti] = arr[j]; foreach (int u in prt) { if (u == -9999) break; Console.Write("{0}\t", u); } Console.WriteLine(); } if (i + N == arr.Length) break; } Console.ReadLine(); } } } 写了2个小时。。 。经调试,,正确
用集合之类的保存M,每次随机从M中取出一个,然后把该数从M中删除,循环N次,OK! //算法 public ArrayList PickNfromM(ArrayList m, int n) { ArrayList result = new ArrayList(); Random rnd = new Random(DateTime.Now.Millisecond); for (int i = 0; i < n; i++) { int index = rnd.Next(m.Count); result.Add(m[index]); m.RemoveAt(index); } return result; } //调用 private void button1_Click(object sender, EventArgs e) { ArrayList al = new ArrayList(); for (int i = 0; i < 20; i++) { al.Add(i); } al = PickNfromM(al, 20); listBox1.Items.Clear(); for (int i = 0; i < al.Count; i++) { listBox1.Items.Add(al[i].ToString()); } }
class DataInfo{ public int Index; public int Data; } private static void PrintDataInfoList(List<DataInfo> diList, int count) { for (int i = 0; i < count; i++) { Console.Write(diList[i].Data + "\t"); } Console.WriteLine(); } private static void Collocate(List<int> dataList, int dataSize, int needSize) { if(dataSize < needSize || dataSize > dataList.Count) { return; } List<DataInfo> tempList = new List<DataInfo>(); for (int i = 0; i < needSize; i++) { tempList.Add(new DataInfo()); } tempList[0].Index = 1; tempList[0].Data = dataList[0]; int dataIndex = 1; int tempIndex = 0; while (true) { if (tempIndex == needSize - 1) { PrintDataInfoList(tempList, needSize); tempIndex--; //dataIndex ++; } else if (dataIndex == dataSize) { tempIndex--; if (tempIndex == -2) { break; } else { dataIndex = tempList[tempIndex + 1].Index; } continue; } else { tempIndex++; tempList[tempIndex].Data = dataList[dataIndex]; tempList[tempIndex].Index = dataIndex + 1; dataIndex++; } } } static void Main(string[] args) { int N = 5; int M = 3; List<int> dataList = new List<int>(); Random rand = new Random(DateTime.Now.Millisecond); for(int i= 0; i < N; i++) { dataList.Add(rand.Next(100)); } Collocate(dataList, N, M);
条件限制不明确:
1.M里有没有重复?
2.N能大于M么?
1:不重复(m中的集合没有重复数字)
2:n<m
从M和里取N个,分N+2个步骤来
1.先从M里取1个,再从M-1个里取N-1个
2.先从M里取2个,再从M-2里取N-2个
N.先从M里取N个,再从M-N里取0个
N+1.去掉前面结果里的重复组
N+2.输出结果OK,现在你要问了,从M-1个里去N-1个怎么取?
同样的,相当于先从 M-1个里取1个,再从M-2个里取N-2个
从M-2个去取N-2个怎么取?递归下去,直到N-(N-1),这个时候后就变成了从M-(N-1)里取N-(N-1)个,问题解决
using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] array ={ 2,3,5,8,9,7,0,34,29,11};
int n=4;
Method(array,n);
}
public static void Method(int[] arr, int N)
{
int[] prt = new int[100];
for (int i = 0; i < prt.Length; i++)
{
prt[i] = -9999;
} for (int i = 0; i < arr.Length; i++)
{
int prti = 0;
int k;
for (k = i; k < N + i - 1; k++, prti++)
{
prt[prti] = arr[k];
}
for (int j = k; j < arr.Length; j++)
{
prt[prti] = arr[j];
foreach (int u in prt)
{
if (u == -9999) break;
Console.Write("{0}\t", u);
}
Console.WriteLine(); }
if (i + N == arr.Length) break; }
Console.ReadLine();
}
}
}
写了2个小时。。
。经调试,,正确
public ArrayList PickNfromM(ArrayList m, int n)
{
ArrayList result = new ArrayList();
Random rnd = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < n; i++)
{
int index = rnd.Next(m.Count);
result.Add(m[index]);
m.RemoveAt(index);
}
return result;
} //调用
private void button1_Click(object sender, EventArgs e)
{
ArrayList al = new ArrayList();
for (int i = 0; i < 20; i++)
{
al.Add(i);
} al = PickNfromM(al, 20); listBox1.Items.Clear();
for (int i = 0; i < al.Count; i++)
{
listBox1.Items.Add(al[i].ToString());
}
}
class DataInfo{
public int Index;
public int Data;
} private static void PrintDataInfoList(List<DataInfo> diList, int count)
{
for (int i = 0; i < count; i++)
{
Console.Write(diList[i].Data + "\t");
}
Console.WriteLine();
} private static void Collocate(List<int> dataList, int dataSize, int needSize)
{
if(dataSize < needSize || dataSize > dataList.Count)
{
return;
} List<DataInfo> tempList = new List<DataInfo>();
for (int i = 0; i < needSize; i++)
{
tempList.Add(new DataInfo());
} tempList[0].Index = 1;
tempList[0].Data = dataList[0]; int dataIndex = 1;
int tempIndex = 0;
while (true)
{
if (tempIndex == needSize - 1)
{
PrintDataInfoList(tempList, needSize);
tempIndex--;
//dataIndex ++;
}
else if (dataIndex == dataSize)
{
tempIndex--;
if (tempIndex == -2)
{
break;
}
else
{
dataIndex = tempList[tempIndex + 1].Index;
}
continue; }
else
{
tempIndex++;
tempList[tempIndex].Data = dataList[dataIndex];
tempList[tempIndex].Index = dataIndex + 1;
dataIndex++;
}
}
} static void Main(string[] args)
{
int N = 5;
int M = 3;
List<int> dataList = new List<int>(); Random rand = new Random(DateTime.Now.Millisecond);
for(int i= 0; i < N; i++)
{
dataList.Add(rand.Next(100));
} Collocate(dataList, N, M);
Console.Read(); }
也写了一份