在二维数组中找到对角线

时间:2011-12-07 17:33:47

标签: c# c++ c builder

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

我知道如何声明数组并让用户填充它,但我不确定偶数元素在主对角线的确意味着什么。

我知道我可以通过说:

找出一个数字
if  n / 2 == 0 

一旦我报告了对角线中偶数元素的总和,我想用数字替换数组中的所有0值。

7 个答案:

答案 0 :(得分:3)

对角线表示x和y坐标相同的所有位置 如果你的数组包含:

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

然后对角线以粗体显示。

答案 1 :(得分:2)

假设数组是正方形:

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;
}

另外,如果你有一个家庭作业问题,下一次添加“家庭作业”标签:P

答案 2 :(得分:0)

这听起来像是家庭作业 - 不过我会帮忙:)

因此,如果您有一个2D数组,并且为了找到对角线值的总和,您将知道两个值的索引将匹配,以便为您提供每个对角线值。

要迭代这些,您可以使用一个简单的循环来总结每个对角线值,如下所示:

//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,例如: < / p>

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
    }
}

答案 3 :(得分:0)

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

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;

答案 4 :(得分:0)

我怀疑主对角线是以坐标0,0开头的那个。

要用0代替0元素,你可以这样做:

if(array [i,j] == 0)array [i,j] == 1;

答案 5 :(得分:0)

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]);
          }
       }
    }

答案 6 :(得分:0)

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

            //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();
相关问题