对矩阵中的选定数字求和
Summing Selected Numbers in a Matrix
我正在尝试使用 for 循环对矩阵的某些元素求和。我成功地对矩阵的所有元素求和,但无法对 selected 的元素求和。这是我的代码:
using System;
namespace debugginglab
{
class Program
{
static void Main(string[] args)
{
int[,] matrix = { { 5, 4, 3, 2, 1 },
{ 1, 3, 4, 6, 5 },
{ 6, 3, 2, 1, 0 },
{ 2, 5, 7, 11,20,},
{ 13,17,19,23,25} };
int sum = summing(matrix);
Console.WriteLine(toplam);
Console.ReadKey();
}
static int summing(int[,] m)
{
int sum = 0;
for (int i = 0; i < m.GetLength(0); ++i)
for (int j = 0; j < m.GetLength(1); ++j)
toplam += m[i, j];
return sum;
Console.ReadKey();
}
}
}
现在我的问题是:例如,我想对第二列的 select 3,3,5 求和,找到 11 结果。我怎样才能用 for loop 对它们进行选择和求和?谢谢
这里您有一个 basic 版本的程序,它使用 for
循环对给定矩阵的选定部分求和。
请注意,它不会为您提供任何类型的溢出/类型/等安全 - 这取决于您。
using System;
public class Program
{
public static void Main(string[] args)
{
int[,] matrix = { { 5, 4, 3, 2, 1 },
{ 1, 3, 4, 6, 5 },
{ 6, 3, 2, 1, 0 },
{ 2, 5, 7, 11,20,},
{ 13,17,19,23,25} };
Console.WriteLine("Start column (from 0)");
var fromColumn = Int32.Parse(Console.ReadLine());
Console.WriteLine("End column (from 0)");
var toColumn = Int32.Parse(Console.ReadLine());
Console.WriteLine("Start row (from 0)");
var fromRow = Int32.Parse(Console.ReadLine());
Console.WriteLine("end row (from 0)");
var toRow = Int32.Parse(Console.ReadLine());
int sum = summing(matrix, fromColumn, toColumn, fromRow, toRow);
Console.WriteLine("Sum is: " + sum);
Console.ReadLine();
}
static int summing(int[,] m, int fromColumn, int toColumn, int fromRow, int toRow)
{
int sum = 0;
for(int i = fromRow; i <= toRow; i++)
{
for(int j = fromColumn; j <= toColumn; j++)
{
sum+= m[i,j];
}
}
return sum;
}
}
例如您的 post(3、3、1 的总和)中提到的,您需要输入开始列为 1,结束列为 1,开始行为 1,结束行为 3。
我正在尝试使用 for 循环对矩阵的某些元素求和。我成功地对矩阵的所有元素求和,但无法对 selected 的元素求和。这是我的代码:
using System;
namespace debugginglab
{
class Program
{
static void Main(string[] args)
{
int[,] matrix = { { 5, 4, 3, 2, 1 },
{ 1, 3, 4, 6, 5 },
{ 6, 3, 2, 1, 0 },
{ 2, 5, 7, 11,20,},
{ 13,17,19,23,25} };
int sum = summing(matrix);
Console.WriteLine(toplam);
Console.ReadKey();
}
static int summing(int[,] m)
{
int sum = 0;
for (int i = 0; i < m.GetLength(0); ++i)
for (int j = 0; j < m.GetLength(1); ++j)
toplam += m[i, j];
return sum;
Console.ReadKey();
}
}
}
现在我的问题是:例如,我想对第二列的 select 3,3,5 求和,找到 11 结果。我怎样才能用 for loop 对它们进行选择和求和?谢谢
这里您有一个 basic 版本的程序,它使用 for
循环对给定矩阵的选定部分求和。
请注意,它不会为您提供任何类型的溢出/类型/等安全 - 这取决于您。
using System;
public class Program
{
public static void Main(string[] args)
{
int[,] matrix = { { 5, 4, 3, 2, 1 },
{ 1, 3, 4, 6, 5 },
{ 6, 3, 2, 1, 0 },
{ 2, 5, 7, 11,20,},
{ 13,17,19,23,25} };
Console.WriteLine("Start column (from 0)");
var fromColumn = Int32.Parse(Console.ReadLine());
Console.WriteLine("End column (from 0)");
var toColumn = Int32.Parse(Console.ReadLine());
Console.WriteLine("Start row (from 0)");
var fromRow = Int32.Parse(Console.ReadLine());
Console.WriteLine("end row (from 0)");
var toRow = Int32.Parse(Console.ReadLine());
int sum = summing(matrix, fromColumn, toColumn, fromRow, toRow);
Console.WriteLine("Sum is: " + sum);
Console.ReadLine();
}
static int summing(int[,] m, int fromColumn, int toColumn, int fromRow, int toRow)
{
int sum = 0;
for(int i = fromRow; i <= toRow; i++)
{
for(int j = fromColumn; j <= toColumn; j++)
{
sum+= m[i,j];
}
}
return sum;
}
}
例如您的 post(3、3、1 的总和)中提到的,您需要输入开始列为 1,结束列为 1,开始行为 1,结束行为 3。