将整数列表打印为矩阵

时间:2018-11-13 14:39:19

标签: c# list

我有9个整数的列表,我想将列表中的所有元素打印为矩阵3,3。而且我必须避免每行末尾出现不必要的空格。 可以使用String.Join吗? 谢谢。

这是我的代码:

int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();

int[][] matrix = new int[input[0]][];

for (int i = 0; i < input[0]; i++)
{
    int[] line = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();

    matrix[i] = line;
}

List<int> arr = new List<int>(9);
List<int> arr1 = new List<int>(9);

arr = Enumerable.Repeat(0, 9).ToList();

//for (int i = 0; i < 9 ; i++) sum[i%3, i/3] = 0;

for (int row = 0; row < input[0] - 2; row++)
{
    for (int col = 0; col < input[1] - 2; col++)
    {
        arr1.Add(matrix[row][col]);
        arr1.Add(matrix[row][col + 1]);
        arr1.Add(matrix[row][col + 2]);
        arr1.Add(matrix[row + 1][col]);
        arr1.Add(matrix[row + 1][col + 1]);
        arr1.Add(matrix[row + 1][col + 2]);
        arr1.Add(matrix[row + 2][col]);
        arr1.Add(matrix[row + 2][col + 1]);
        arr1.Add(matrix[row + 2][col + 2]);

        if (arr1.Sum() > arr.Sum())
        {
            arr = arr1.Select(a => a).ToList();
        }
        arr1.Clear();
    }
}

Console.WriteLine($"Sum = {arr.Sum()} ");

// print the list as a matrix

4 个答案:

答案 0 :(得分:4)

这就是我使用String.Join打印它的方式:

List<int> asd = new List<int> {1,2,3,4,5,6,7,8,9};

for (int i = 0; i < asd.Count; i +=3)
{
    Console.WriteLine(string.Join(" ",asd.Skip(i).Take(3)));
}

说明:以3的步长移动。跳过等于步长的数量,然后取3组合一行矩阵。

答案 1 :(得分:2)

那是你的意思吗?

    string output = string.Empty;
    List<int> myList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    int counter = 0;
    foreach (int value in myList)
    {
        output += value.ToString();
        counter++;
        if (counter % 3 == 0)
        {
            Console.WriteLine(output);
            output = string.Empty;
        }
    }

答案 2 :(得分:2)

现在,您已粘贴代码。无论如何,我创建了示例以打印3x3矩阵。

class Program
{
    static void Main(string[] args)
    {
        StringBuilder stringBuilder = new StringBuilder();
        List<int> numbers = new List<int>() {1,2,3,4,5,6,7,8,9 };


        for (int i = 0; i <3; i++)
        {
            int index = i * 3;
            stringBuilder.AppendFormat("{0}{1}{2}", numbers[index], numbers[index + 1], numbers[index + 2]);
            stringBuilder.AppendLine();
        }

        Console.Write(stringBuilder.ToString());
        Console.ReadLine();
    }
}

答案 3 :(得分:2)

您应该重新考虑接受的答案,因为许多项目的表现都较差。

这可能与您当前的项目数无关,但仍然听到我的警告。

我运行了以下代码段:

var sb = new StringBuilder();
for (int i = 0; i < asd.Count; i +=3)
    sb.AppendLine(string.Join(" ", asd.Skip(i).Take(3)));
Console.WriteLine(sb.ToString());

使用StringBuilder删除循环中每个项目的相关时间Console.WriteLine();

此方法需要756,115ms才能完成,其中包含1,000,000个项目。

像这样创建asd列表:

var asd = Enumerable.Range(0, 1000000).ToList();

到目前为止,所有给出的其他答案的效果都会更好。

之所以接受的解决方案执行效果不佳的原因是因为在循环中调用了.Skip(),实际上并没有跳过并直接转到此Position,而是一次又一次地循环列表,直到它到达了这一点。

我的解决方法是:

Console.WriteLine(string.Concat(asd.Select((x, i) => (i + 1) % 3 != 0 ? x + " " : x + Environment.NewLine)));

8,610ms中执行相同的任务

为了完善:

  • Wojtek的解决方案需要:7,932ms
  • Nirmal Subedi'解决方案需要:8,088ms

注意:

对其进行了更改,以便它使用StringBuilder来构建字符串,并且仅将字符串输出到控制台一次,而不是循环调用Console.WriteLine()

这是我完整的测试例程:

var asd = Enumerable.Range(0, 1000000).ToList();

var sw1 = new Stopwatch();
sw1.Start();
Console.WriteLine(string.Concat(asd.Select((x, i) => (i + 1) % 3 != 0 ? x + " " : x + Environment.NewLine)));
sw1.Stop();

var sw2 = new Stopwatch();
sw2.Start();

var sb1 = new StringBuilder();
for (int i = 0; i < asd.Count; i += 3)
    sb1.AppendLine(string.Join(" ", asd.Skip(i).Take(3)));
Console.WriteLine(sb1.ToString());

sw2.Stop();

var sw3 = new Stopwatch();
sw3.Start();

var sb2 = new StringBuilder();
int counter = 0;
string output = "";
foreach (int value in asd)
{
    counter++;
    if (counter % 3 == 0)
    {
        output += value;
        sb2.AppendLine(output);
        output = string.Empty;
    }
    else
        output += value + " ";
}
Console.WriteLine(sb2.ToString());
sw3.Stop();

var sw4 = new Stopwatch();
sw4.Start();

var sb3 = new StringBuilder();
for (int i = 0; i <asd.Count / 3; i++)
{
    int index = i * 3;
    sb3.AppendFormat("{0} {1} {2}", asd[index], asd[index + 1], asd[index + 2]);
    sb3.AppendLine();
}
Console.WriteLine(sb3.ToString());
sw4.Stop();

Console.WriteLine("MySolution: " + sw1.ElapsedMilliseconds);
Console.WriteLine("Mong Zhu's Solution: " + sw2.ElapsedMilliseconds);
Console.WriteLine("Wojtek's Solution: " + sw3.ElapsedMilliseconds);
Console.WriteLine("Nirmal Subedi's Solution: " + sw4.ElapsedMilliseconds);

Console.ReadKey();