Приветствую всех, студенту задали задание в котором он просит ему помочь в написании алгоритма. И так у нас дано условие: Нужно вывести на экран 111111, 111122, 111133, 111144, 112211, 112222, 112233, 112244, 113311, 113322…444444. важно, чтобы алгоритм работал для 50 и более цифр подряд. Не будем терять времени и приступим к написанию кода.
Рассмотрим код:
//изучаем C# на nookery.ru
class Program
{
static void Main(string[] args)
{
Console.Write("Введите парное число больше нуля : ");
int number = int.Parse(Console.ReadLine());
GetNumbers(number);
Console.ReadLine();
}
public static void GetNumbers(int value)
{
if (value % 2 == 1)
{
Console.WriteLine("Число не парное!!!");
return;
}
List<int> values = Enumerable.Range(1, value).Select(x => x = 1).ToList();
while (!values.All<int>(x => x == 4))
{
for (int i = 2; i < 5; i++)
{
Console.WriteLine("Значение : " + string.Join("", values));
values[values.Count - 1] = i;
values[values.Count - 2] = i;
}
Console.WriteLine("Значение : " + string.Join("", values));
int index = values.FindIndex(new Predicate<int>((x) => { return x == 4; }));
while (index != -1)
{
if (values.All<int>(x => x == 4))
break;
if (index != 0)
{
values[index] = 1;
values[index + 1] = 1;
values[index - 1] = values[index - 1] + 1;
values[index - 2] = values[index - 2] + 1;
Console.WriteLine("Значение : " + string.Join("", values));
index = values.FindIndex(new Predicate<int>((x) => { return x == 4; }));
}
else
{
index = values.FindIndex(index + 2, new Predicate<int>((x) => { return x == 4; }));
if (index != -1)
{
values[index] = 1;
values[index + 1] = 1;
values[index - 1] = values[index - 1] + 1;
values[index - 2] = values[index - 2] + 1;
index = -1;
}
break;
}
}
}
}
}
