在二维数组中求对角线

本文关键字:对角线 二维数组 | 更新日期: 2023-09-27 18:17:09

我有一个用户输入值的二维数组。我需要找到数组对角线上偶数元素的和。

我知道如何声明数组并让用户填充它,但我不确定甚至主对角线中的元素的真正含义。

我知道我可以找出一个数字是否是偶数:

if  n / 2 == 0 

一旦我报告了对角线上偶数元素的和,我想将数组中的所有0值替换为1。

在二维数组中求对角线

对角线是指所有x和y坐标相同的地方如果你的数组包含:

1 3 8 5
33 9 7
4 4 5 7
5 1 7 4

假设数组为正方形:

int sum = 0;
for(int i = 0; i < numOfArrayRows; i++)
{
    //Use the mod operator to find if the value is even.
    if(array[i][i] % 2 == 0)
        sum += array[i][i];
    //Change 0's to ones
    for(int j = 0; j < numOfArrayCols; j++)
        if(array[i][j] == 0)
            array[i][j] = 1;
}

另外,下次如果你有家庭作业问题,请加上"Homework"标签:p

使用二维数组非常简单,因为您不需要任何索引魔术:

int a[N][N] = ...;
int sum = 0;
for(int i=0; i<N; ++i)
    if(a[i][i] % 2 == 0) //or a[i] & 1, if you like, just check if it's dividable by 2
        sum += a[i][i];

这段c++代码在C或c#中应该没有那么大的不同,但你应该明白这一点。同样,第二个问题也可以像这样简单:

int a[M][N] = ...;
for(i=0; i<M; ++i)
    for(j=0; j<N; ++j)
        if(a[i][j] == 0)
            a[i][j] = 1;

我猜主对角线是从坐标(0,0)开始的。

要将0个元素替换为1,您可以这样做:

如果(数组(i, j) = = 0)数组(i, j) = = 1;

这听起来像家庭作业-但我会帮助:)

所以如果你有一个二维数组,为了找到对角线值的和,你会知道两个值的下标匹配,以便为你提供每个对角线值。

要遍历这些值,可以使用一个简单的循环来对每个对角线值求和,如下所示:

//Your Sum
int sum = 0;
//This will iterate and grab all of the diagonals
//You don't need to iterate through every element as you only need
//the diagonals.
for(int i = 0; i < sizeOfArray; i++)
{
   //This will add the value of the first, second, ... diagonal value to your sum
   sum += array[i,i];
}

要将每个值从0设置为1,可以遍历数组的每个元素并检查值是否为0,然后将该值设置为1,例如:

for(int i = 0; i < sizeOfArray; i++)
{
    for(int j = 0; j < sizeOfArray; j++)
    {
         //Check if this value is 0;
         //If it is 0, set it to 1, otherwise continue
    }
}

int[,] array = new int[,] {{1,2,3},
                           {4,5,6},
                           {7,8,9}};
    //Suppose you want to find 2,5,8
    for(int row = 0; row < 3; row++)
    {
       for(int column = 0; column < 3; column++)
       {
          if((row == 0 && column == 1) || (row == 1 && column == 1) || (row == 2 && column == 1))
          {
             Console.WriteLine("Row: {0} Column: {1} Value: {2}",row + 1, column + 1, array[row, column]);
          }
       }
    }

这是您需要的代码,没有太多解释:

            //Read the size of the array, you can get it from .Count() if you wish
        int n = Convert.ToInt32(Console.ReadLine());
        int[][] a = new int[n][];
        //Reading all the values and preparing the array (a)
        for (int a_i = 0; a_i < n; a_i++)
        {
            string[] a_temp = Console.ReadLine().Split(' ');
            a[a_i] = Array.ConvertAll(a_temp, Int32.Parse);
        }
        //performing the operation (google what diagonal matrix means)
        int PrimarySum = 0, SecondarySum = 0;
        for (int i = 0; i < n; i++)
        {
            //The If condition is to skip the odd numbers
            if (a[i][i] % 2 == 0) { PrimarySum += a[i][i]; }
            //For the reverse order
            int lastelement = a[i].Count() - 1 - i;
            if (a[i][lastelement] % 2 == 0) { SecondarySum += a[i][lastelement]; }
        }
        //Get the absolute value
        Console.WriteLine(Math.Abs(PrimarySum - SecondarySum).ToString());
        Console.ReadKey();

使用c#,我将这样做:

int[,] matrix = new int[4, 4];
for (int i = 0; i < matrix.GetLength(0); i++)
{
    for (int j = 0; j < matrix.GetLength(1); j++) {
        if (matrix[i, i] == matrix[j, j]) { 
            matrix[i, j] = 1;
        }
        else
        {
            matrix[i, j] = 0;
        }
    }
}