Заполнение массива против часовой стрелки - C#

Узнай цену своей работы

Формулировка задачи:

Помогите, пожалуйста... В данном случае идет заполнение массива по часовой стрелке (кроме полей, они должны быть нулями):
Листинг программы
  1. int[,] t = new int[12, 12];
  2. int n = 0;
  3. for (int z = 0; z < 5; z++)
  4. {
  5. for (int i = 1 + z; i < 10 - z; i++)//верх
  6. {
  7. n++;
  8. t[1 + z, i] = n;
  9. }
  10. for (int i = 1 + z; i < 10 - z; i++)//право
  11. {
  12. n++;
  13. t[i, 10 -z] = n;
  14. }
  15. for (int i = 10 - z; i > 1 + z; i--) //низ
  16. {
  17. n++;
  18. t[10 - z, i] = n;
  19. }
  20. for (int i = 10 - z; i > 1 + z; i--)//лево
  21. {
  22. n++;
  23. t[i, 1 + z] = n;
  24. }
  25. }
  26. for (int i = 0; i < 12; i++)
  27. {
  28. for (int j = 0; j < 12; j++)
  29. {
  30. Console.Write(t[i, j] + " ");
  31. }
  32. Console.WriteLine();
  33. }
Но это не важно, т.к. у меня это получилось. А вот заполнить массив ИЗ ЦЕНТРА до краев против часовой стрелки у меня никак не выходит, все перепробовал и по-разному подставлял, так нормально не получилось...

Решение задачи: «Заполнение массива против часовой стрелки»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace WindowsFormsApplication57
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public ArrayDrawer AD = new ArrayDrawer();
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.             this.DoubleBuffered = true;
  20.             Timer t = new Timer() { Interval = 30 };
  21.             t.Tick += (s, e) => { AD.Currentnumber++; Invalidate(); if (AD.Currentnumber == 100) t.Stop(); };
  22.             t.Start();
  23.         }
  24.  
  25.         private void Form1_Paint(object sender, PaintEventArgs e)
  26.         {
  27.             AD.Draw(e.Graphics);
  28.         }
  29.     }
  30.  
  31.     public class ArrayDrawer
  32.     {
  33.         public int Currentnumber { get { return currentNumber; } set {
  34.                 if (value > SquareSize * SquareSize) throw new ArgumentException("Число больше максимума");
  35.                 if (value < 0) throw new ArgumentException("Число меньше нуля");
  36.                 else currentNumber = value; } }
  37.         public Point Location;
  38.         public int OnePointWidth { get; set; } = 20;
  39.         public int FontSize { get; set; } = 8;
  40.         public int SquareSize { get; set; } = 10;
  41.  
  42.         public int[,] CurrentArray
  43.         {
  44.             get { return (int[,])arr.Clone(); }
  45.         }
  46.  
  47.         private int[,] arr;
  48.         private int currentNumber=1;
  49.         public void Draw(Graphics g)
  50.         {
  51.             arr = new int[SquareSize+1, SquareSize+1];
  52.             Point lastUsed = Point.Empty;
  53.             int direction=0;
  54.             for (int i = 1; i < Currentnumber+1; i++)
  55.             {
  56.                 Point current = Point.Empty;
  57.                 if(lastUsed == Point.Empty)
  58.                 {
  59.                     current = new Point(arr.GetLength(0)/2,arr.GetLength(1)/2);
  60.                 }
  61.                 else
  62.                 {
  63.                     switch (direction)
  64.                     {
  65.                         case 0:
  66.                             if(arr[lastUsed.X+1,lastUsed.Y] != 0)
  67.                             {
  68.                                 current = new Point(lastUsed.X, lastUsed.Y + 1);
  69.                             }
  70.                             else
  71.                             {
  72.                                 current = new Point(lastUsed.X + 1, lastUsed.Y);
  73.                                 direction++;
  74.                             }
  75.                             break;
  76.  
  77.                         case 1:
  78.                             if (arr[lastUsed.X, lastUsed.Y-1] != 0)
  79.                             {
  80.                                 current = new Point(lastUsed.X+1, lastUsed.Y);
  81.                             }
  82.                             else
  83.                             {
  84.                                 current = new Point(lastUsed.X, lastUsed.Y - 1);
  85.                                 direction++;
  86.                             }
  87.                             break;
  88.  
  89.                         case 2:
  90.                             if (arr[lastUsed.X-1, lastUsed.Y] != 0)
  91.                             {
  92.                                 current = new Point(lastUsed.X, lastUsed.Y-1);
  93.                             }
  94.                             else
  95.                             {
  96.                                 current = new Point(lastUsed.X - 1, lastUsed.Y);
  97.                                 direction++;
  98.                             }
  99.                             break;
  100.  
  101.                         case 3:
  102.                             if (arr[lastUsed.X, lastUsed.Y + 1] != 0)
  103.                             {
  104.                                 current = new Point(lastUsed.X - 1, lastUsed.Y);
  105.                             }
  106.                             else
  107.                             {
  108.                                 current = new Point(lastUsed.X, lastUsed.Y + 1);
  109.                                 direction=0;
  110.                             }
  111.                             break;
  112.                     }
  113.                 }
  114.                 arr[current.X, current.Y] = i;
  115.                 lastUsed = current;
  116.                 Point toDraw = new Point(Location.X + OnePointWidth * current.X, Location.Y + OnePointWidth * current.Y);
  117.                 g.DrawString(i.ToString(), new Font(FontFamily.GenericSerif, FontSize), Brushes.Black, toDraw);
  118.                 g.DrawRectangle(Pens.Red, new Rectangle(toDraw, new Size(OnePointWidth, OnePointWidth)));
  119.             }
  120.         }
  121.     }
  122. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

7   голосов , оценка 4.286 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы