int[] num = new int[19] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
List<int> lists = new List<int>();
List<int> lists2 = new List<int>(); //保存那些加起来和是20的数
foreach (int i in num)
{
lists.Add(i);
}
int sum = 0; //和
int index = 0; //临时变量
Random ra = new Random();
do
{
index = ra.Next(lists.Count);
if (sum + lists[index] <= 20)
{
sum = sum + lists[index];
lists2.Add(lists[index]);
lists.Remove(lists[index]);
}
else
{
lists.Remove(lists[index]);
}
//重新赋值
if (lists.Count == 0)
{
foreach (int i in num)
{
lists.Add(i);
lists2.Clear();
}
sum = 0;
}
}
while (sum != 20 && lists.Count > 0);
Console.WriteLine(sum); |