Вывод результата в виде ромба - C#
Формулировка задачи:
Добрый день, есть вывод чисел в таком порядке:
1 (1 РАЗ)
222 (3 РАЗА)
33333 (5 раз)
... (n раз)
33333 (5 раз)
222 (3 РАЗА)
1 (1 РАЗ)
Подскажите, как сделать горизонтальную табуляция, чтобы выводилось ввиде ромба
Листинг программы
- using System;
- namespace RecursiveAlgorithms
- {
- public class Program
- {
- static void Main(string[] args)
- {
- Console.Write("Введите число N: ");
- int N = int.Parse(Console.ReadLine());
- DrawPicture(1, 1, N);
- Console.ReadLine();
- }
- static void DrawPictureRow(int value, int count)
- {
- for (int index = 0; index < count; ++index)
- {
- Console.Write(value);
- }
- Console.WriteLine();
- }
- static void DrawPicture(int value, int count, int N)
- {
- if (count < N)
- {
- DrawPictureRow(value, count);
- DrawPicture(value + 1, count + 2, N);
- DrawPictureRow(value, count);
- }
- else
- {
- DrawPictureRow(value, N);
- }
- }
- }
- }
Решение задачи: «Вывод результата в виде ромба»
textual
Листинг программы
- using System;
- namespace RecursiveAlgorithms
- {
- public class Program
- {
- static void Main(string[] args)
- {
- int N, Num; //
- do
- {
- Console.Write ("Enter num (1 to 9): ");
- N = Convert.ToInt32 (Console.ReadLine());
- } while (N < 1 || N > 9);
- Num = N;
- //Прорисовка верхней части
- DrowingUpper(N, Num);
- //Прорисовка нижней части
- Num = 1;
- DrowingBottom (N, Num);
- Console.ReadKey(true);
- }
- public static void DrowingUpper (int N, int Num)
- {
- if (Num > 1)
- DrowingUpper (N, Num - 1);
- for (int i = 0; i < N - Num; i++)
- Console.Write (" ");
- for (int i = 0; i < Num * 2 - 1; i++)
- Console.Write (Num);
- Console.WriteLine();
- }
- public static void DrowingBottom (int N, int Num)
- {
- if (Num < N)
- DrowingBottom (N, Num + 1);
- if (N != Num)
- {
- for (int i = 1; i < N + 1 - Num; i++)
- Console.Write (" ");
- for (int i = 0; i < Num * 2 - 1; i++)
- Console.Write (Num);
- Console.WriteLine();
- }
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д