Определите, является ли матрица магическим квадратом - C# (197880)

Узнай цену своей работы

Формулировка задачи:

срочно помогите пожалуйста, сдавать через часов 10
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Курсовая_работа
{
    class Program
    {
        static void Main(string[] args)
        {
 
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.BackgroundColor = ConsoleColor.White; Console.Clear();
 
            Console.Write("Число строк: ");
            int x = Int32.Parse(Console.ReadLine());
            Console.Write("Число столбцов: ");
            int y = Int32.Parse(Console.ReadLine());
            int[,] a = new int[x, y];
 
            Console.WriteLine("Заполняем массив элементами =>");
            for (int i = 0; i < x; i++)
            {
                for (int j = 0; j < y; j++)
                {
                    Console.Clear();
 
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.BackgroundColor = ConsoleColor.White; Console.Clear();

                    Console.Write("Введите элементами А [" + i + "," + j + "]=> ");
                    a[i, j] = Int32.Parse(Console.ReadLine());
                }
            };
 
            Console.WriteLine("Вывод массива =>");
            Console.WriteLine();
            for (int i = 0; i < x; i++)
            {
                for (int j = 0; j < y; j++)
                {
                    Console.Write(a[i, j] + " ");
                }
                Console.WriteLine();
            }
            int S = 0;
 
            for (int i = 0; i < x; i++)
            {
                for (int j = 0; j < y; j++)
                {
                    S = S + a[i, j];
                }
            }
            
            for (int i = 0; i < x; ++i)
            {
                int sum_strok = 0;
                for (int j = 0; j < y; j++)
 
                    sum_strok = a[i, j] + sum_strok;         
            }
 
            for (int j = 0; j < x; ++j)
            {
                int sum_stolb = 0;
                for (int i = 0; i < y; i++)
                   
                    sum_stolb = a[i, j] + sum_stolb;
            }
 
            int sum_glav = 0;
            for (int j = 0; j < x; j++)
            {
                sum_glav = a[j, j] + sum_glav;
            }
            
            int sum_neglav = 0;
            int n = x;
            for (int i = 0; i < x; i++)
            {
                sum_neglav = a[i, n - i - 1] + sum_neglav;
            }

            Console.WriteLine(); Console.WriteLine();
            Console.ReadLine();
 
                }
            }
        }
не могу вписать выбор ((
 if (SumStrok[0] == SumStolb[0] && flagStrok == arr.Length - 1 && flagStolb == arr.Length - 1)
                Console.WriteLine("Матрица 9-го порядка является магическим квадратом!");
 
            else Console.WriteLine("Матрица не является магическим квадратом.");
))))) не могу найти правильный ответ

Решение задачи: «Определите, является ли матрица магическим квадратом»

textual
Листинг программы
using System;
 
namespace _1
{
    class Program
    {
        static bool isMagig (int[,] arr)
        {
            if (arr.GetLength(0) != arr.GetLength(1))
                return false;
 
            for (int i = 0; i < arr.GetLength(0); i++) {
                int summRow = 0;
                int summCol = 0;
                int summDiag1 = 0;
                int summDiag2 = 0;
                for (int j = 0; j < arr.GetLength(1); j++) {
                    summRow += arr[i,j];
                    summCol += arr[j,i];
                    summDiag1 += arr[j,j];
                    summDiag2 += arr[arr.GetLength(0) - 1 - j, arr.GetLength(1) - 1 - j];
                }
                if (summRow != summCol || summRow != summDiag1 || summRow != summDiag2)
                    return false;
            }
            return true;
        }
        static void Main()
        {
            int[,] magic = {{16,5,9,4},{3,10,6,15},{2,11,7,14},{13,8,12,1}};
            int[,] notMagic = {{16,5,9,4},{3,11,6,15},{2,11,7,14},{13,8,12,1}};
            Console.WriteLine(isMagig(magic));
            Console.WriteLine(isMagig(notMagic));
            Console.ReadKey(true);
        }
    }
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 3.909 из 5
Похожие ответы