【问题标题】:To check if matrix row is an palindrome (array in array)检查矩阵行是否是回文(数组中的数组)
【发布时间】:2021-11-10 09:16:04
【问题描述】:

任务是我需要检查数组(二维数组)中的数组是否为回文 问题是我在第 40 行遇到“超出范围”异常,无法弄清楚如何告诉计算机在那里进行正确计算:if (arr[i, j] != arr[arr.GetLength(1) - i, j - 1])

我需要将 if (arr[i] != arr[n - i - 1]) 解释为二维数组。 到目前为止,这是我的代码:```

class Program
    {
        static void Main(string[] args)
        {

            Random rnd = new Random();
            int n;
            n = Convert.ToInt32(Console.ReadLine());
            int[,] arr = new int[n, n];


            
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for(int j = 0; j < arr.GetLength(1); j++)
                {
                    arr[i, j] = rnd.Next(1, 15);
                }
            }

            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.WriteLine(arr[i, j]);
                }
            }


            int flag = 0;
            for (int i = 0; i < arr.GetLength(0); i++)
            {

                for (int j = 0; j <= arr.GetLength(1) / 2 && n != 0; j++)
                {
                    if (arr[i, j] != arr[arr.GetLength(1) - i, j - 1])
                    {
                        Console.WriteLine(i);
                        flag = 1;
                        break;
                    }

                }
            }
            if (flag == 1)
            {
                Console.WriteLine("pali");
            }
            else
            {
                Console.WriteLine("not pali");
            }
        }
    }

【问题讨论】:

    标签: c# arrays multidimensional-array palindrome


    【解决方案1】:

    正在寻找这样的东西吗?

    namespace ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[,] arr = new int[,]
                {
                    { 1, 2, 3, 4, 5, 6 },
                    { 1, 2, 3, 4, 5, 6 },
                    { 1, 2, 3, 3, 2, 1 },      // Palindrome
                    { 1, 2, 3, 4, 5, 6 },
                    { 1, 2, 3, 4, 5, 6 }
                };
                PrintArray(arr);
                CheckForPalindrome(arr);
            }
    
            static void PrintArray(int[,] arr)
            {
                for (int i = 0; i < arr.GetLength(0); i++)
                {
                    for (int j = 0; j < arr.GetLength(1); j++)
                    {
                        Console.Write($"{arr[i, j]} ");
                    }
                    Console.WriteLine();
                }
            }
    
            static void CheckForPalindrome(int[,] arr)
            {
                for (int i = 0; i < arr.GetLength(0); i++)
                {
                    var equals = true;
                    for (int j = 0; j < arr.GetLength(1) / 2; j++)
                    {
                        equals = arr[i, j] == arr[i, arr.GetLength(1) - j - 1];
                        if (equals == false)
                            break;
                    }
                    if (equals == true)
                        Console.WriteLine($"Row {i + 1} is palindrome");
                }
            }
        }
    }
    

    【讨论】:

    • 是的!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多