حل مربع جادویی به روش دیگر...با ما باشید
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
{
int[,]a=new int[10,10];
int i,j,n,k=1;
Console.WriteLine("*** Please Enter The Number Between 0 to 9 ***");
n=Convert.ToInt32(Console.ReadLine());
if(n%2==0 ||n>9)
{
Console.WriteLine("\n\n***The Given Number is Not Acceptable!,Try Again ***");
Console.ReadKey();
Environment.Exit(1);
}
else
{
for (i=0;i<=n;i++)
for (j=0;j<=n;j++)
a[i,j]=0;
j=(n/2)+1;
i=1;
a[i,j]=k;
for(k=2;k<=(n*n);k++)
{
i=i-1;
j=j-1;
if((i==0 &&j==0)||(a[i,j]!=0))
{
i=i+2;
j=j+1;
}
else
{
if(i==0)
i=n;
else if(j==0)
j=n;
}
a[i,j]=k;
}
}
Console.WriteLine("\n\n");
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
Console.WriteLine("\t",a[i,j]);
Console.WriteLine("\n\n");
}
Console.ReadKey();
}
}
}
}
- ۹۴/۰۸/۰۹