공부,일/셸 스크립트
달팽이 행렬 출력하기
fromnothing1
2021. 6. 22. 13:33
과제 호우
c# 구현
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
int n = 4;
int num = n * n;
int x =0, y = 0 ;
int[][] Rarray = new int[n][];
for (int i = 0; i < n; i++)
{
Rarray[i] = new int[n];
}
for (int j = n - 1; j > 0 ; j-=2)
{
for (int a = 0; a < 4; a++)
{
for (int i = 0; i < j; i++)
{
switch (a)
{
case 0:
Rarray[y][x] = num;
x++;
num--;
break;
case 1:
Rarray[y][x] = num;
y++;
num--;
break;
case 2:
Rarray[y][x] = num;
x--;
num--;
break;
case 3:
Rarray[y][x] = num;
y--;
num--;
break;
default:
break;
}
}
}
x++;
y++;
}
if ((n%2) == 1)
{
Rarray[y][x] = 1;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write($"{Rarray[i][j],2}");
}
Console.WriteLine("");
}
}
}
}