Консультация № 187392
30.05.2013, 14:17
100.42 руб.
0 6 2
Здравствуйте! Прошу решить задачу:

Дан двумерный массив целых чисел из 4 столбцов и 3 строк. Найти сумму элементов стоящих до первого нуля. Просмотр вести по строкам. Заменить элементы, стоящие после последнего нуля на эту сумму. Вывести массив по строкам до и после замены.

Обсуждение

давно
Профессор
399103
482
30.05.2013, 17:30
общий
это ответ
Здравствуйте, Ушаков Антон Сергеевич!

Код:

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();
}
}
}
}
Неизвестный
30.05.2013, 19:46
общий
это ответ
Здравствуйте, Ушаков Антон Сергеевич!

Код:
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
5
Неизвестный
30.05.2013, 19:47
общий
Заменить элементы, стоящие после ПОСЛЕДНЕГО нуля на эту сумму.
давно
Профессор
399103
482
30.05.2013, 22:53
общий
Действительно, был невнимателен.
давно
Профессор
399103
482
30.05.2013, 22:57
общий
В первой версии были заменены элементы, стоящие после первого нуля. В новой, согласно условию, стоящие после последнего.
Неизвестный
31.05.2013, 21:23
общий
спасибо!я все понял
Форма ответа