我想在C#中以升序对矩阵进行排序而没有任何功能

时间:2018-08-25 05:08:26

标签: c#

输入的矩阵结果是:

                         1    5   48

                         7   11    3

P.S用户可以输入不同于上面的任何数字。

它应该按升序排列:1 3 5 7 11 48

我使用了循环,但无法正常工作

using System;

namespace MainClass
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j, m, n, sum = 0;
            int count = 0;
            Console.Write("The MxN matrix\n");
            Console.Write("Enter the number of rows and columns of the matrix :\n");
            Console.Write("Rows (M): ");
            m = Convert.ToInt32(Console.ReadLine());
            Console.Write("Columns (N): ");
            n = Convert.ToInt32(Console.ReadLine());
            int[,] arr = new int[m, n];
            Console.Write("Enter elements in the first matrix :\n");

            /* Entering matrix elements */

            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write("element - [{0}],[{1}] : ", i, j);
                    arr[i, j] = Convert.ToInt32(Console.ReadLine());

                    /* Calculating odd numbers of the matrix */

                    if (arr[i, j] % 2 != 0)
                    {
                        sum += arr[i, j];
                    }
                }
            }
            Console.Write("\nThe matrix is:\n");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                    Console.Write("{0} ", arr[i, j]);
                Console.Write("\n");
            }
            Console.Write("\nThe sum of odd numbers is: {0}", sum);

            /* Sorting Matrix in ascending order*/

            for (i = 0; i < m; i++)
            {
                for (j = i+1; j < n; j++)
                {
                  for (int j1 = 0; j1 < n; j1++ )
                  {
                    if (arr[i, j] > arr[i, j1]){
                      int temp = arr[i, j];
                      arr[i, j] = arr[i, j1];
                      arr[i, j1] = temp;
                    }
                  }
                }
            }
            Console.Write("\nAscending order: ");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                    Console.Write("{0} ", arr[i, j]);
            }
             Console.Read();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您在这里。

using System;
namespace MainClass
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j, m, n, sum = 0; int count = 0; Console.Write("The MxN matrix\n"); Console.Write("Enter the number of rows and columns of the matrix :\n"); Console.Write("Rows (M): "); m = Convert.ToInt32(Console.ReadLine()); Console.Write("Columns (N): "); n = Convert.ToInt32(Console.ReadLine()); int[,] arr = new int[m, n]; Console.Write("Enter elements in the first matrix :\n");

            /* Entering matrix elements */

            for (j = 0; j < n; j++)
            {
                for (i = 0; i < m; i++)
                {
                    Console.Write("element - [{0}],[{1}] : ", i, j);
                    arr[i, j] = Convert.ToInt32(Console.ReadLine());

                    /* Calculating odd numbers of the matrix */

                    if (arr[i, j] % 2 != 0)
                    {
                        sum += arr[i, j];
                    }
                }
            }
            Console.Write("\nThe matrix is:\n");
            for (j = 0; j < n; j++)
            {
                for (i = 0; i < m; i++)
                    Console.Write("{0} ", arr[i, j]);
                Console.Write("\n");
            }
            Console.Write("\nThe sum of odd numbers is: {0}", sum);

            /* Sorting Matrix in ascending order*/

            for (i = 0; i < arr.Length - 1; i++)
            {
                for (j = i + 1; j < arr.Length; j++)
                {
                    int row1 = i % m;
                    int col1 = i / m;

                    int row2 = j % m;
                    int col2 = j / m;

                    if (arr[row1, col1] > arr[row2, col2])
                    {
                        int temp = arr[row1, col1];
                        arr[row1, col1] = arr[row2, col2];
                        arr[row2, col2] = temp;
                    }

                }
            }


            Console.Write("\nAscending order: ");
            for (j = 0; j < n; j++)
            {
                for (i = 0; i < m; i++)
                    Console.Write("{0} ", arr[i, j]);
            }
            Console.Read();
        }
    }
}