مربع جادویی
Erfan_ 0098 | شنبه, ۹ آبان ۱۳۹۴، ۰۳:۳۳ ب.ظ |
۰ نظر
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Magic_Square
{
class Program
{
static void Main(string[] args)
{
int z, x, i, j, k, nsqr, n, range, ix;
int[,] a1 = new int[100, 100];
int[,] a2 = new int[100, 100];
int[,] matrix = new int[100, 100];
Console.WriteLine("Enter n:");
n = Convert.ToInt32(Console.ReadLine());
i = 0;
j = n / 2;
k = 1;
nsqr = n * n;
if (n % 2 == 0)
{
while (k <= nsqr)
{
matrix[j] = k;
j++;
i--;
if (k % n == 0)
{
i += 2;
j--;
}//end if
else
{
if (j == n) j -= n;
else if (i < 0) i += n;
}//end else
k++;
}//end while
}//end if
else
{
ix = 1;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
{
a1[j] = ((i + 1) % 4) / 2;
a2[j] = ((i + 1) % 4) / 2;
matrix[j] = ix;
ix++;
}//end for
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (a1[j] == a2[i,j])
matrix[j] = n * n + 1 - matrix[j];
}//end else
Console.WriteLine("*** Magic___Square: *** ");
for (z = 0; z < n; z++)
{
for (x = 0; x < n; x++)
Console.WriteLine(matrix[z,x] );
Console.WriteLine(" ");
Console.WriteLine("");
}//end for
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Magic_Square
{
class Program
{
static void Main(string[] args)
{
int z, x, i, j, k, nsqr, n, range, ix;
int[,] a1 = new int[100, 100];
int[,] a2 = new int[100, 100];
int[,] matrix = new int[100, 100];
Console.WriteLine("Enter n:");
n = Convert.ToInt32(Console.ReadLine());
i = 0;
j = n / 2;
k = 1;
nsqr = n * n;
if (n % 2 == 0)
{
while (k <= nsqr)
{
matrix[j] = k;
j++;
i--;
if (k % n == 0)
{
i += 2;
j--;
}//end if
else
{
if (j == n) j -= n;
else if (i < 0) i += n;
}//end else
k++;
}//end while
}//end if
else
{
ix = 1;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
{
a1[j] = ((i + 1) % 4) / 2;
a2[j] = ((i + 1) % 4) / 2;
matrix[j] = ix;
ix++;
}//end for
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (a1[j] == a2[i,j])
matrix[j] = n * n + 1 - matrix[j];
}//end else
Console.WriteLine("*** Magic___Square: *** ");
for (z = 0; z < n; z++)
{
for (x = 0; x < n; x++)
Console.WriteLine(matrix[z,x] );
Console.WriteLine(" ");
Console.WriteLine("");
}//end for
Console.ReadKey();
}
}
}
- ۹۴/۰۸/۰۹