English 中文(简体)
我怎么能够从分界线获得数字?
原标题:How can I get the sum of numbers from a diagonal line?
  • 时间:2010-09-28 14:59:07
  •  标签:
  • c#

这里指向底线的分校:

public int sumarDiagonal()
{
    int x = 0;
    for (int F = 0; F < Filas; F++)
    {
        for (int c = 0; c < Columnas; c++)
        {
            if (F == c)
            {
                x += m[F,c];
            }
        }
    }
    return x;
}

我怎么能从上权利到底线?

最佳回答

Your original code with two nested loops is not very efficient. Better do it like this:

public int sumarDiagonal() 
{ 
 int x = 0; 
 for (int i = 0; i < Math.Min(Filas,Columnas); ++i) 
  x += m[i,i]; 
 return x; 
} 

public int sumarAntiDiagonal() 
{ 
 int x = 0; 
 for (int i = 0; i < Math.Min(Filas,Columnas); ++i) 
  x += m[Filas - 1 - i,i]; 
 return x; 
} 
问题回答

您实际上不需要考虑每个系数:

public int sumarDiagonal()
    {
        int x = 0;
        int length = Math.Min(Filas,Columnas); // Can deal with rectangular cases
        for (int i = 0; i < length; i++)
            x += m[i,length-1-i];
        return x;
    }

正好相反:

public int sumarDiagonal()
{
   int x = 0;
   for (int F = 0; F < Filas; F++)
   {
      x += m[F,Filas-F-1];
   }
   return x;
}

这假定有一阵列,例如10x10,第一点是[0、9],第二点[1、8]等。

如何替换

if (F == c)

iii

if (F + c + 1 == Filas)

Edit: 更新以核算0基阵列

   public int diagX()
        {
            int x;
            x = 0;

        for (f = 1; f <= filas; f++)
          {
           for (c = columnas; c >= 1; c--)
               {
                   if (f > c || f < c)
                   {
                       x += a[f, c];
                   }
                   else
                   {
                       continue;
                   }
                }
          }
        return x;
        }

for example this code : if you have 1 , 2 3 , 6 and you want to across diag you must take a[2, 1] you will get 2. and at the looping you ve give continue on the else because after get value 2. it will be a[2, 2] because f == c as false then you break not come back to first loop. why f==c is false because if you want search across diag of matrix you cant get value from f == c. for example: if matrix above we loop but can equals f and c. we can get: (a[2, 1] = 2) + (a[2, 2] = 6) = 8 nah! you can see the big false at this code.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签