آموزش های کاربردی در دنیا رایانه و وب

مربع جادویی

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();

        }
    }
}

  • Erfan_ 0098

نظرات  (۰)

هیچ نظری هنوز ثبت نشده است

ارسال نظر

ارسال نظر آزاد است، اما اگر قبلا در بیان ثبت نام کرده اید می توانید ابتدا وارد شوید.
شما میتوانید از این تگهای html استفاده کنید:
<b> یا <strong>، <em> یا <i>، <u>، <strike> یا <s>، <sup>، <sub>، <blockquote>، <code>، <pre>، <hr>، <br>، <p>، <a href="" title="">، <span style="">، <div align="">
تجدید کد امنیتی