Сгенерируйте серию случайных чисел из 0, 1, 2 так, чтобы количество двоек было равно количеству нулей
Сгенерируйте серию случайных чисел из 0, 1, 2 так, чтобы количество двоек было равно количеству нулей без массива.
Помогите пожалуйста доделать вот эту задачу! У меня не выходит парное количество двое и нулей.
Вот что я сделал :
while (true)
{
Console.Write("Введите кол-во цифр: ");
int num = int.Parse(Console.ReadLine());
if (num < 3)
{
Console.WriteLine("Не меньше трёх!!!!!");
continue;
}
Random rnd = new Random();
int a = rnd.Next(1, (num + 1) / 2);
int b = a;
int c = num - a * 2;
int count = num;
while (count > 0)
{
int s = rnd.Next(0, 3 + 1);
if (s == 0 && c > 0)
{
Console.Write(0 + " ");
c--;
count--;
}
if (s == 1 && a > 0)
{
Console.Write(1 + " ");
a--;
count--;
}
if (s == 2 && b > 0)
{
Console.Write(2 + " ");
b--;
count--;
}
}
Console.WriteLine();
}
Буду благодарен за любой ответ!
Ответы (3 шт):
Автор решения: Aziz Umarov
→ Ссылка
вот так попробуйте
while (true)
{
Console.Write("Введите кол-во цифр: ");
int num = int.Parse(Console.ReadLine());
// можно и 3 только вот чтоб было обязательно один 1 и вариант ниже в
// комбинации
// можно и 2 только чтоб было либо 0,2 либо 2,0
// либо 1,1
// и вариант только 1
if (num < 0)
{
Console.WriteLine("Введите положительное число!!!!!");
continue;
}
Random rnd = new Random();
int diff = 0;
int count = num;
while (count > 0)
{
int s = rnd.Next(0, 3 + 1);
if (s == 0 && (count - Math.Abs(diff + 1)) > 0)
{
Console.Write(0 + " ");
diff++;
count--;
}
if (s == 1 && (count - Math.Abs(diff)) > 0)
{
Console.Write(1 + " ");
count--;
}
if (s == 2 && (count - Math.Abs(diff - 1)) > 0)
{
Console.Write(2 + " ");
diff--;
count--;
}
}
Console.WriteLine();
}
Автор решения: Pavel Popov
→ Ссылка
Эх, чем еще в субботу не заняться, как не школьными задачками :-)
var random = new Random();
while (true)
{
Console.Write("Введите кол-во цифр: ");
int length = int.Parse(Console.ReadLine());
if (length < 3)
{
Console.WriteLine("Не меньше трёх!!!!!");
continue;
}
var zeroCount = 0;
var twoCount = 0;
var totalLength = 0;
while (totalLength < length)
{
var curNum = random.Next(0, 3);
var curSpace = length - totalLength;
var needTwo = zeroCount > twoCount ? zeroCount - twoCount : 0;
var needZero = twoCount > zeroCount ? twoCount - zeroCount : 0;
var k = 1;
if (needZero == 0 && needTwo == 0)
k = 2;
if (curNum == 0 && needZero == 0 && needZero + needTwo + (needTwo - needZero + k) > curSpace)
continue;
if (curNum == 2 && needTwo == 0 && needZero + needTwo + (needZero - needTwo + k) > curSpace)
continue;
if (curNum == 1 && length - totalLength <= needZero + needTwo)
continue;
if (curNum == 0)
zeroCount++;
if (curNum == 2)
twoCount++;
Console.Write($"{curNum} ");
totalLength++;
}
Console.ReadKey();
Console.WriteLine("");
}
Автор решения: tym32167
→ Ссылка
Чисто ради разнообразия, добавлю немного итераторов
Console.Write("Введите кол-во цифр: ");
int num = int.Parse(Console.ReadLine());
var rnd = new Random();
var twoAndZero = rnd.Next(num/2+1);
var onesCount = Math.Max(0, num - twoAndZero * 2);
var result = Enumerable.Repeat("1", onesCount)
.Concat(Enumerable.Repeat("0", twoAndZero))
.Concat(Enumerable.Repeat("2", twoAndZero))
.OrderBy(x => rnd.Next())
.Aggregate((x, y) => x + y);
Console.WriteLine(result);