Заполнение массива против часовой стрелки - C#
Формулировка задачи:
Помогите, пожалуйста...
В данном случае идет заполнение массива по часовой стрелке (кроме полей, они должны быть нулями):
Но это не важно, т.к. у меня это получилось. А вот заполнить массив ИЗ ЦЕНТРА до краев против часовой стрелки у меня никак не выходит, все перепробовал и по-разному подставлял, так нормально не получилось...
Листинг программы
- int[,] t = new int[12, 12];
- int n = 0;
- for (int z = 0; z < 5; z++)
- {
- for (int i = 1 + z; i < 10 - z; i++)//верх
- {
- n++;
- t[1 + z, i] = n;
- }
- for (int i = 1 + z; i < 10 - z; i++)//право
- {
- n++;
- t[i, 10 -z] = n;
- }
- for (int i = 10 - z; i > 1 + z; i--) //низ
- {
- n++;
- t[10 - z, i] = n;
- }
- for (int i = 10 - z; i > 1 + z; i--)//лево
- {
- n++;
- t[i, 1 + z] = n;
- }
- }
- for (int i = 0; i < 12; i++)
- {
- for (int j = 0; j < 12; j++)
- {
- Console.Write(t[i, j] + " ");
- }
- Console.WriteLine();
- }
Решение задачи: «Заполнение массива против часовой стрелки»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication57
- {
- public partial class Form1 : Form
- {
- public ArrayDrawer AD = new ArrayDrawer();
- public Form1()
- {
- InitializeComponent();
- this.DoubleBuffered = true;
- Timer t = new Timer() { Interval = 30 };
- t.Tick += (s, e) => { AD.Currentnumber++; Invalidate(); if (AD.Currentnumber == 100) t.Stop(); };
- t.Start();
- }
- private void Form1_Paint(object sender, PaintEventArgs e)
- {
- AD.Draw(e.Graphics);
- }
- }
- public class ArrayDrawer
- {
- public int Currentnumber { get { return currentNumber; } set {
- if (value > SquareSize * SquareSize) throw new ArgumentException("Число больше максимума");
- if (value < 0) throw new ArgumentException("Число меньше нуля");
- else currentNumber = value; } }
- public Point Location;
- public int OnePointWidth { get; set; } = 20;
- public int FontSize { get; set; } = 8;
- public int SquareSize { get; set; } = 10;
- public int[,] CurrentArray
- {
- get { return (int[,])arr.Clone(); }
- }
- private int[,] arr;
- private int currentNumber=1;
- public void Draw(Graphics g)
- {
- arr = new int[SquareSize+1, SquareSize+1];
- Point lastUsed = Point.Empty;
- int direction=0;
- for (int i = 1; i < Currentnumber+1; i++)
- {
- Point current = Point.Empty;
- if(lastUsed == Point.Empty)
- {
- current = new Point(arr.GetLength(0)/2,arr.GetLength(1)/2);
- }
- else
- {
- switch (direction)
- {
- case 0:
- if(arr[lastUsed.X+1,lastUsed.Y] != 0)
- {
- current = new Point(lastUsed.X, lastUsed.Y + 1);
- }
- else
- {
- current = new Point(lastUsed.X + 1, lastUsed.Y);
- direction++;
- }
- break;
- case 1:
- if (arr[lastUsed.X, lastUsed.Y-1] != 0)
- {
- current = new Point(lastUsed.X+1, lastUsed.Y);
- }
- else
- {
- current = new Point(lastUsed.X, lastUsed.Y - 1);
- direction++;
- }
- break;
- case 2:
- if (arr[lastUsed.X-1, lastUsed.Y] != 0)
- {
- current = new Point(lastUsed.X, lastUsed.Y-1);
- }
- else
- {
- current = new Point(lastUsed.X - 1, lastUsed.Y);
- direction++;
- }
- break;
- case 3:
- if (arr[lastUsed.X, lastUsed.Y + 1] != 0)
- {
- current = new Point(lastUsed.X - 1, lastUsed.Y);
- }
- else
- {
- current = new Point(lastUsed.X, lastUsed.Y + 1);
- direction=0;
- }
- break;
- }
- }
- arr[current.X, current.Y] = i;
- lastUsed = current;
- Point toDraw = new Point(Location.X + OnePointWidth * current.X, Location.Y + OnePointWidth * current.Y);
- g.DrawString(i.ToString(), new Font(FontFamily.GenericSerif, FontSize), Brushes.Black, toDraw);
- g.DrawRectangle(Pens.Red, new Rectangle(toDraw, new Size(OnePointWidth, OnePointWidth)));
- }
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д