using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
// массив
int[,] array = new int[3, 4] { {1, 2, 3, 4},
{5, 0, 7, 1},
{0, 2, 4, 8} };
// выводим исходный
print(array);
// сумма до нуля
int sum = 0;
int firstNullPos = -1;
int lastNullPos = -1;
int curPos = 0;
foreach (int i in array)
{
if (i == 0)
{
if (firstNullPos < 0)
firstNullPos = curPos;
lastNullPos = curPos;
}
if (firstNullPos < 0)
sum += i;
++curPos;
}
// заменяем элементы
// если нулевой элемент был
if (firstNullPos >= 0)
{
int height = array.GetLength(0);
int width = array.GetLength(1);
for (int pos = lastNullPos + 1; pos < array.Length; ++pos)
{
int i = pos / width;
int j = pos % width;
array[i, j] = sum;
}
}
// выводим изменённый
System.Console.WriteLine();
print(array);
Console.ReadLine();
}
static void print(int[,] array)
{
for (int i = 0; i < array.GetLength(0); ++i)
{
for (int j = 0; j < array.GetLength(1); ++j)
System.Console.Write(array[i, j] + "\t");
System.Console.WriteLine();
}
}
}
}
using System;
namespace N_187392
{
class Program
{
static void Main (string[] arg)
{
int[,] arr = new int[3, 4]
{{1, 2, 3, 4},
{5, 0, 7, 1},
{0, 2, 4, 8}};
new Program ().Run (arr);
}
int lastZero = -1;
void Run (int[,] arr)
{
Print ("Исходный массив:", arr);
// Поиск последнего нуля и суммирование
int index = 0;
int summa = 0;
foreach (var item in arr) {
if (item == 0) {
lastZero = index;
}
if (lastZero == -1) {
summa += item;
}
++index;
}
// Если не нашли нулей
if (lastZero == -1) {
Console.WriteLine ("Массив не содержит нулевых элементов");
} else {
Console.WriteLine ("Сумма: {0}", summa);
++lastZero;
int rows = arr.GetLength (0);
int cols = arr.GetLength (1);
int c = lastZero % cols;
for (int r=lastZero/cols; r<rows; ++r) {
while (c<cols) {
arr [r, c] = summa;
++c;
}
c = 0;
}
Print ("Результат:", arr);
}
}
void Print (string msg, int[,] arr)
{
Console.WriteLine ("{0}", msg);
int rows = arr.GetLength (0);
int cols = arr.GetLength (1);
for (int r=0; r<rows; ++r) {
for (int c=0; c<cols; ++c) {
Console.Write ("{0} ", arr [r, c]);
}
Console.WriteLine ();
}
}
}
}
Исходный массив:
1 2 3 4
5 0 7 1
0 2 4 8
Сумма: 15
Результат:
1 2 3 4
5 0 7 1
0 15 15 15
Если Вы уже зарегистрированы на Портале - войдите в систему, если Вы еще не регистрировались - пройдите простую процедуру регистрации.