繁体   English   中英

2D数组到1D数组C#

[英]2d array to 1d array C#

我有两种将随机2D数组(mхn或mхm)转换为1d数组的算法。 我想知道是否有一种方法可以使它们沿相反方向工作并将结果转换为保存数字顺序的1d数组。 这是我的程序的完整代码和一张图片,以查看我的两种算法如何工作。 在此处输入图片描述代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using static System.Math;


namespace MyProgram
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.Write("Enter number of rows:");
            int n = int.Parse(Console.ReadLine());
            Console.Write("Enter number of columns:");
            int m = int.Parse(Console.ReadLine());
            int[] arr1 = new int[n * m];
            int[,] arr2 = new int[n, m];
            int choice; 
            do
            {
                Console.WriteLine("Select option:");
                Console.WriteLine("\t1: Diagonal");
                Console.WriteLine("\t2: Spiral");
                Console.WriteLine("\t3: Exit");
                Console.Write("Your selection: ");
                choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        {
                            SetArray(arr2);
                            PrintArray(arr2);
                            Diagonal(arr2, arr1); 
                            PrintArray(arr1);
                            break;
                        }
                    case 2:
                        {
                            SetArray(arr2);
                            PrintArray(arr2);
                            Spiral(arr2, arr1);
                            PrintArray(arr1);
                            break;
                        }
                }
                Console.WriteLine();
                Console.WriteLine();
            } while (choice != 5);
        }
           static void Diagonal(int[,] array2, int[] array1)
        {
            int k = 0;
            int row = 0;
            int col = 0;
            while (k < array1.Length)
            {

                array1[k] = array2[row, col];
                if ((row + col) % 2 == 0)
                {
                    if ((row == 0) && (col != array2.GetLength(1) - 1)) { col++; }
                    else
                    {
                        if (col == array2.GetLength(1) - 1) { row++; }
                        else { row--; col++; }
                    }
                }
                else
                {
                    if ((col == 0) && (row != array2.GetLength(0) - 1)) { row++; }
                    else
                    {
                        if (row == array2.GetLength(0) - 1) { col++; }
                        else { row++; col--; }
                    }

                }
                k += 1;
            }
        }

        private static void Spiral(int[,] array2, int[] array1)
        {
            int lengthX = array2.GetLength(0);
            int lengthY = array2.GetLength(1);
            int Product = lengthX * lengthY;
            int CorrectY = 0;
            int CorrectX = 0;
            int Count = 0;
            while (lengthX > 0 && lengthY > 0)
            {
                for (int j = CorrectY; j < lengthY && Count < Product; j++)
                {
                    array1[Count] = array2[CorrectX, j];
                    Count++ ;
                }
                CorrectX++;
                for (int i = CorrectX; i < lengthX && Count < Product; i++)
                {
                    array1[Count] = array2[i, lengthY - 1];
                    Count++ ;
                }
                if (lengthY > 0 && lengthX > 0) lengthY-- ;
                else break;
                for (int j = lengthY - 1; j >= CorrectY && Count < Product; j--)
                {
                    array1[Count] = array2[lengthX - 1, j];
                    Count++ ;
                }
                if (lengthY > 0 && lengthX > 0) lengthX-- ;
                else break;
                for (int i = lengthX - 1; i >= CorrectX && Count < Product; i--)
                {
                    array1[Count] = array2[i, CorrectY];
                    Count++ ;
                }

                CorrectY++;
            }

        }

        public static void SetArray(int[,] arr)
        {
            Random r = new Random();
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    arr[i, j] = r.Next(11, 99);
                }
            }
        }

        public static void SetArray(int[] arr)
        {
            Random r = new Random();
            for (int i = 0; i < arr.Length; i++)
            {

                arr[i] = r.Next(11, 99);

            }
        }

        public static void PrintArray(int[] arr)
        {
            Console.Write("print 1d array:");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine();
        }

       public static void PrintArray(int[,] arr)
        {
            Console.WriteLine("print 2d array:");
            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();
            }
        } 
    }
}

这似乎对我来说是向后Diagonal

static void BackwardDiagonal(int[,] array2, int[] array1) {

    int k = 0;
    int row = 0;
    int col = 0;
    while (k < array1.Length) {
        array2[row, col] = array1[k];  // just swap sides of the assignment...
        if ((row + col) % 2 == 0) {
            if ((row == 0) && (col != array2.GetLength(1) - 1)) { col++; } else {
                if (col == array2.GetLength(1) - 1) { row++; } else { row--; col++; }
            }
        } else {
            if ((col == 0) && (row != array2.GetLength(0) - 1)) { row++; } else {
                if (row == array2.GetLength(0) - 1) { col++; } else { row++; col--; }
            }
        }
        k += 1;
    }
}

对于向后Spiral

private static void BackwardSpiral(int[,] array2, int[] array1)
{
    int lengthX = array2.GetLength(0);
    int lengthY = array2.GetLength(1);
    int Product = lengthX * lengthY;
    int CorrectY = 0;
    int CorrectX = 0;
    int Count = 0;
    while (lengthX > 0 && lengthY > 0)
    {
        for (int j = CorrectY; j < lengthY && Count < Product; j++)
        {
            array2[CorrectX, j] = array1[Count];  // just swap sides of the assignment...
            Count++ ;
        }
        CorrectX++;
        for (int i = CorrectX; i < lengthX && Count < Product; i++)
        {
            array2[i, lengthY - 1] = array1[Count];
            Count++ ;
        }
        if (lengthY > 0 && lengthX > 0) lengthY-- ;
        else break;
        for (int j = lengthY - 1; j >= CorrectY && Count < Product; j--)
        {
            array2[lengthX - 1, j] = array1[Count];
            Count++ ;
        }
        if (lengthY > 0 && lengthX > 0) lengthX-- ;
        else break;
        for (int i = lengthX - 1; i >= CorrectX && Count < Product; i--)
        {
            array2[i, CorrectY] = array1[Count];
            Count++ ;
        }

        CorrectY++;
    }
}

我还将此添加到switch语句中以实现它:

case 4: 
    {
        SetArray(arr2);
        PrintArray(arr2);
        Diagonal(arr2, arr1);
        PrintArray(arr1);
        int[,] arr3 = new int[n, m];  // new blank array to fill with arr1
        BackwardDiagonal(arr3, arr1);  // fill arr3 from backward Diagonal algorithm
        PrintArray(arr3);
        break;
    }
case 5: 
    {
        SetArray(arr2);
        PrintArray(arr2);
        Spiral(arr2, arr1);
        PrintArray(arr1);
        int[,] arr3 = new int[n, m];  // new blank array to fill with arr1
        BackwardSpiral(arr3, arr1);  // fill arr3 from backward Spiral algorithm
        PrintArray(arr3);
        break;
    }

在执行此操作时,还请确保同时使用} while (choice != 3); do循环的末尾,以便您可以退出程序!

实际上,您要问的很简单。 只需更改您的算法方法即可接收像这样的int rowsint cols和委托

delegate void Apply(int row, int col, int index);

然后更换arr2.GetLength(0)rowsarr2.GetLength(1)cols ,和数组元素分配与委托调用。

这是更新的Diagonal方法(您可以对其他方法进行相同操作):

static void Diagonal(int rows, int cols, Apply action)
{
    int k = 0;
    int row = 0;
    int col = 0;
    int length = rows * cols;
    while (k < length)
    {
        action(row, col, k);
        if ((row + col) % 2 == 0)
        {
            if ((row == 0) && (col != cols - 1)) { col++; }
            else
            {
                if (col == cols - 1) { row++; }
                else { row--; col++; }
            }
        }
        else
        {
            if ((col == 0) && (row != rows - 1)) { row++; }
            else
            {
                if (row == rows - 1) { col++; }
                else { row++; col--; }
            }
        }
        k += 1;
    }
}

和用法:

SetArray(arr2);
PrintArray(arr2);
Diagonal(n, m, (r, c, i) => arr1[i] = arr2[r, c]);
PrintArray(arr1);
// Inverse
var arr3 = new int[n, m];
Diagonal(n, m, (r, c, i) => arr3[r, c] = arr1[i]);
PrintArray(arr3);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM