C#程序以升序排列n * n(二维数组)(首先排列列然后排列行) [英] C# program to arrange n*n (two dimensional array) in ascending order ( arrange the columns first and then the rows)

查看:148
本文介绍了C#程序以升序排列n * n(二维数组)(首先排列列然后排列行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例



输入:



n = 3



example

input :

n=3

2  2  1 
3  53 4
32 5  3





输出:





output:

1  2  2
3  3 54
4 32 53





__________________

好​​的,这是我能想到的全部内容!但我确实不能正常工作!

为什么你要报告我的问题?!!!我不理解!!





__________________
ok, here's ALL I CAN THINK OF! BUT IM PRETTY SURE ITS NOT WORKING!
WHY WOULD YOU GUYS REPORT MY QUESTION?!!!I DON UNDERSTAND!!

static void Main()
{
   int n = int.parse(console.ReadLine());

   int [,] array1 = new int [n,n];

   for(int i=0;i<array1.getlength(0);++i)>
   for(int j=0;j<array1.getlength(1);++j)>
   array1[i,j]=int.parse(console.readline());

   for(int j=0;j<array1.getlength(1);++j)>
   for(int i=0;i<array1.getlength(0);++i)>
   Array.Sort<int>(array1)

   for(int i=0;i<array1.getlength(0);++j)>
   for(int j=0;j<array1.getlength(1);++i)>
   Array.Sort<int>(array1)
}

推荐答案

检查: http://www.informit .com / guides / content.aspx?g = dotnet& seqNum = 151 [ ^ ]

PS:这叫做排序,而不是安排。
Check this: http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=151[^]
PS: this is called sorting, not arranging.


有一件事你应该注意:对一个锯齿状数组进行排序,一个数组形式SomeArray [] []按列很容易用Linq做;对二维数组进行排序,按列的形式SomeArray [,]数组需要嵌套迭代。请参阅Marc Gravell对StackOverFlow的评论:[ ^ ],另请参阅:[ ^ ]。



从技术上讲,使用Linq在多维数组中实现列排序的问题与它没有实现/支持IEnumerable的事实有关;一个锯齿状的数组呢。



我会通过使用泛型集合和Linq来解决这个问题。这是一个快速草图:
One thing you should be aware of: sorting a jagged array, an array of the form SomeArray[][] by columns is very easy to do with Linq; sorting a two-dimensional Array, an array of the form SomeArray[,] by columns requires nested iteration. See Marc Gravell's comments on this on StackOverFlow: [^], and, also, see: [^].

Technically, the problem with achieving column sort in a multi-dimensional array using Linq is related to the fact that it does not implement/support IEnumerable; a jagged array does.

I would approach this problem by making use of generic collections, and Linq. Here's a quick sketch:
// required
using System.Collections.Generic;
using System.Linq;

private void TestSort()
{
    int[,] ints = new int[3,3]
    {
        {2, 2, 1}, {3, 53, 4}, {32, 5, 3}
    };

    List<List<int>> columns = new List<List<int>>();

    for (int i = 0; i < 3; i++)
    {
        List<int> column = new List<int>();

        for (int j = 0; j < 3; j++) column.Add(ints[j,i]);

        column.Sort();

        columns.Add(column);
    }

    List<List<int>> rows = new List<List<int>>();

    for (int i = 0; i < 3; i++)
    {
        List<int> row = new List<int>();

        for (int j = 0; j < 3; j++) row.Add(columns[j][i]);

        row.Sort();

        rows.Add(row);
    }

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++) ints[i, j] = rows[i][j];
    }            
}

我建议您设置断点,并执行此代码并检查不同列表的内容,因为它们已定义并已排序。



是的,这是一个蛮力的解决方案,而且,经过努力,我确信可以让它更精简,更精简。

I suggest you set break-points, and execute this code and examine the contents of the different Lists as they are defined, and sorted.

Yes, this is a "brute force" solution, and, with effort, I am sure one could make it "leaner and meaner."


这篇关于C#程序以升序排列n * n(二维数组)(首先排列列然后排列行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆