C# 用C计算矩阵的东西对角线#

C# 用C计算矩阵的东西对角线#,c#,C#,任何人都可以帮我做一个项目 我想用C#编写一个可以填充矩阵的代码 0 0 0 0 5 0 0 0 4 0 0 0 3 0 0 0 2 0 0 0 1 0 0 0 0 我正在做一个星期,但我没有找到解决办法 谢谢这是您的解决方案。至少试着理解它是如何工作的 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextes

任何人都可以帮我做一个项目 我想用C#编写一个可以填充矩阵的代码

0 0 0 0 5 0 0 0 4 0 0 0 3 0 0 0 2 0 0 0 1 0 0 0 0 我正在做一个星期,但我没有找到解决办法
谢谢

这是您的解决方案。至少试着理解它是如何工作的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var n = 5;
            var result = Enumerable.Range(0,n*n).GroupBy(k=> k/n).Select(g=> g.Select(k=> (k % n == (n-1-k/n)) ? k%n+1 : 0).ToArray()).ToArray();
            foreach(var r in result)
            {
                foreach(var a in r)
                {
                    Console.Write(" {0}", a);
                }
                Console.WriteLine();
            }
        }
    }
}
以下是我的尝试:

int[,] arr = new int[n,n];
for(int i = 0; i < n; i++)
{
    for(int j = 0; j < n; j++)
    {
        if(i+j-1 == n)
        {
            arr[i,j] = j+1;
        }
        else
        {
            arr[i,j] = 0;
        }
        Console.Write(arr[i,j] + " ");
    }
Console.WriteLine();
}
int[,]arr=新的int[n,n];
对于(int i=0;i

我还没有检查这个特定的代码是否有效,但是类似的东西应该可以解决这个问题。

谢谢,我找到了一个解决方案

using System;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] arr = new int[5, 5];
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    if (i + j  == 4)
                    {
                        arr[i, j] = j + 1;
                    }
                    else
                    {
                        arr[i, j] = 0;
                    }
                    Console.Write(arr[i, j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
}
使用系统;
名称空间控制台EAPP5
{
班级计划
{
静态void Main(字符串[]参数)
{
int[,]arr=新的int[5,5];
对于(int i=0;i<5;i++)
{
对于(int j=0;j<5;j++)
{
如果(i+j==4)
{
arr[i,j]=j+1;
}
其他的
{
arr[i,j]=0;
}
Console.Write(arr[i,j]+“”);
}
Console.WriteLine();
}
}
}
}

这是下一周的解决方案:var result=Enumerable.Range(0,25).GroupBy(k=>k/5).Select(g=>g.Select(k=>(k%5==(5-1-k/5))?5-k/5:0.ToArray()).ToArray();另一年的解决方案。谢谢你,但我不明白
using System;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] arr = new int[5, 5];
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    if (i + j  == 4)
                    {
                        arr[i, j] = j + 1;
                    }
                    else
                    {
                        arr[i, j] = 0;
                    }
                    Console.Write(arr[i, j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
}