1

Имеется класс булева матрица.Нужно выбрать матрицы с равным количеством true(1) в каждой строке. Пока такое, дальше не знаю, что делать: var findSymbolRequest = from item in list where item.CountOfUnit != 0 select item;

class BoolMatrix
{
private bool[][] arr;

public BoolMatrix()
{
    arr = new bool[2][];
    for (int i = 0; i < arr.Length; i++)
    {
        arr[i] = new bool[2];
    }
    Console.WriteLine("Введите матрицу");
    for (int i = 0; i < arr.Length; i++)
    {
        for (int j = 0; j < arr[i].Length; j++)
        {

            String s = Console.ReadLine();
            switch (s)
            {
                case "0":
                    {
                        arr[i][j] = false;
                        break;
                    }
                default:
                    {
                        arr[i][j] = true;
                        break;
                    }
            }
        }
    }
}

public int CountOfUnit()
{
    int c = 0;
    for (int i = 0; i < arr.Length; i++)
    {
        for (int j = 0; j < arr.Length; j++)
        {
            if (arr[i][j]) c++;
        }
    }
    return c;
}
}

1 ответ 1

0
public BoolMatrix()
  private int TrueCountInRow(aRow)
  {
    int result = 0;
    for (int j = 0; j < arr[aRow].Length; j++)
    {
      if (arr[aRow][j]) 
        result++;
    }
    return result;
  }

  public bool SameTrueCountInRows()
  {
    int? count = 0;
    for (int i = 0; i < arr.Length; i++)
    {
      if (count.HasValue) {
        if (count != TrueCountInRow(i))
          return false;
      }
      else
      {
        count = TrueCountInRow(i);
      }
    }
    return true;
  }
  ...
}

foreach (var bm in list.Where(m => m.SameTrueCountInRows()) bm.Print()

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.