Заменить нулевые элементы массива единицами - C#
Формулировка задачи:
Доброго времени суток. Нужно, все элементы массива, начиная с первого(он изначально задан равным 1) с шагом S заменить на единицу.
Вот мой код, что-то я не так там намудрил по-моему с циклами:
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int N, M, S, K, L, i, j, P;
- Console.WriteLine("Введите количество серых мышей: ");
- N = int.Parse(Console.ReadLine());
- Console.WriteLine("Введите количество белых мышей: ");
- M = int.Parse(Console.ReadLine());
- Console.WriteLine("Введите какую по счету мышь съедает кошка: ");
- S = int.Parse(Console.ReadLine());
- //Console.WriteLine("Вот так мыши были расположены: ");
- int[,] m = new int[N, M];
- for (i = 0; i < N; i++)
- {
- for (j = 0; j < M; j++)
- {
- m[i, j] = 0;
- Console.Write(m[i, j] + "\t");
- }
- Console.WriteLine();
- }
- for (i = 0; i < N; i+=S)
- {
- for (j = 0; j < M; j+=S)
- {
- m[i, j] = 1;
- Console.Write(m[i, j] + "\t");
- }
- Console.WriteLine();
- }
- //Console.WriteLine("Сколько серых мышей осталось в живых?");
- //K = int.Parse(Console.ReadLine());
- // Console.WriteLine("Сколько белых мышей осталось в живых?");
- //L = int.Parse(Console.ReadLine());
- //P = (N + M) - (K + L);
- }
- }
- }
Решение задачи: «Заменить нулевые элементы массива единицами»
textual
Листинг программы
- так работает
- [CSHARP]
- using System;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- #region Заполнение стартовых данных
- int S;
- Console.WriteLine("Введите количество серых мышей: ");
- int count_gray = int.Parse(Console.ReadLine());
- Console.WriteLine("Введите количество белых мышей: ");
- int count_wite = int.Parse(Console.ReadLine());
- Console.WriteLine("Введите какую по счету мышь съедает кошка: ");
- S = int.Parse(Console.ReadLine());
- Console.WriteLine("Сколько серых мышей осталось в живых?");
- int K = int.Parse(Console.ReadLine());
- Console.WriteLine("Сколько белых мышей осталось в живых?");
- int L = int.Parse(Console.ReadLine());
- int P = (count_wite + count_gray) - (K + L);
- Console.WriteLine("Вот так мыши были расположены: ");
- int[] massiv = new int[count_gray + count_wite];
- #endregion
- #region выводим стартовый массив
- for (int j = 0; j < massiv.Length; j++)
- {
- massiv[j] = 0;
- Console.Write(massiv[j] + "\t");//
- }
- Console.WriteLine();
- Console.WriteLine();
- #endregion
- #region Кошка ест мышек
- int i = 0;//номер итерации
- int g = 0;//номер съедаемой мышки
- int t = 0;//сдвиг при повторе
- while (true)
- {
- if (i == P)
- break;
- else if (g >= massiv.Length)
- {
- g = g-massiv.Length;//Начинаем с того числа на которое превысили длинну массива
- if (g % 3 == 0 && massiv.Length % 3 == 0)
- g++;
- t++;
- }
- massiv[g] = 1;
- i++;
- g += S;
- }
- #endregion
- string[] colors = new string[massiv.Length];
- int d = 0;//Подсчет серых мышей оставшихся в живых
- int r = 0;//Подсчет белых мышей оставшихся в живых
- int l = 0;//Серые мышы до кошки
- int y = 0;//Белые быши до кошки
- for (i = 0; i < massiv.Length; i++)
- {
- if (massiv[i] == 0 && d < K)
- {
- colors[i] = "Серая";
- d++;
- l++;
- continue;
- }
- if (massiv[i] == 0 && r < L)
- {
- colors[i] = "Белая";
- r++;
- y++;
- continue;
- }
- }
- for ( i = 0; i < massiv.Length; i++)
- {
- if (colors[i] == null && l < count_gray)
- {
- colors[i] = "Серая";
- l++;
- }
- if (colors[i] == null && y < count_wite)
- {
- colors[i] = "Белая";
- y++;
- }
- }
- for (int j = 0; j < massiv.Length; j++)
- {
- Console.Write(massiv[j] + "\t");
- }
- Console.WriteLine();
- Console.WriteLine();
- string live = "";
- for (i = 0; i < colors.Length; i++)
- {
- if (massiv[i] == 0)
- live = "Жива";
- else
- live = "Съедена";
- if (colors[i] == "Серая")
- Console.ForegroundColor = ConsoleColor.Gray;
- Console.WriteLine(colors[i] + " " + live + " " + i);
- Console.ForegroundColor = ConsoleColor.White;
- }
- Console.ReadLine();
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д