Сравнение двух массивов - C# (183833)

Узнай цену своей работы

Формулировка задачи:

Решаю задачку, следующий код не проходит тестирование:
Листинг программы
  1. static void Main(string[] args)
  2. {
  3. //var a = new[] { 121, 144, 19, 161, 19, 144, 19, 11 };
  4. //var b = new[] { 121, 14641, 20736, 361, 25921, 361, 20736, 361 };
  5. int[] a = new int[] { 121, 144, 19, 161, 19, 144, 19, 11 };
  6. int[] b = new int[] { 11 * 11, 121 * 121, 144 * 144, 19 * 19, 161 * 161, 19 * 19, 144 * 144, 19 * 19 };
  7. Console.WriteLine(Mul(a, b));
  8. Console.ReadKey();
  9. }
  10. static bool Mul(int[]a,int[] b)
  11. {
  12. //if (a.Where(i => i == 0).Count() == a.Length
  13. // || b.Where(i => i == 0).Count() == b.Length)
  14. // return false;
  15. if (a == null && b == null)
  16. return false;
  17. if ((a != null && b == null) || (a == null && b != null))
  18. return false;
  19. if (a.Length != b.Length)
  20. return false;
  21. bool check = a.Where(i => !b.Contains(i * i)).Count() > 0 ? false : true;
  22. if (!check)
  23. check = b.Where(i => !a.Contains(i * i)).Count() > 0 ? false : true;
  24. return check;
  25. }
Задача в следующем, даны 2 массива, квадраты членов первого должны содержаться во втором массиве, если все квадраты содержатся во втором, нужно вернуть true.
Листинг программы
  1. Given two arrays a and b write a function comp(a, b) (compSame(a, b) in Clojure) that checks whether the two arrays have the "same" elements, with the same multiplicities. "Same" means, here, that the elements in b are the elements in a squared, regardless of the order.
  2. Examples
  3. Valid arrays
  4.  
  5. a = [121, 144, 19, 161, 19, 144, 19, 11]
  6. b = [121, 14641, 20736, 361, 25921, 361, 20736, 361]
  7.  
  8. comp(a, b) returns true because in b 121 is the square of 11, 14641 is the square of 121, 20736 the square of 144, 361 the square of 19, 25921 the square of 161, and so on. It gets obvious if we write b's elements in terms of squares:
  9.  
  10. a = [121, 144, 19, 161, 19, 144, 19, 11]
  11. b = [11*11, 121*121, 144*144, 19*19, 161*161, 19*19, 144*144, 19*19]
  12.  
  13. Invalid arrays
  14.  
  15. If we change the first number to something else, comp may not return true anymore:
  16.  
  17. a = [121, 144, 19, 161, 19, 144, 19, 11]
  18. b = [132, 14641, 20736, 361, 25921, 361, 20736, 361]
  19.  
  20. comp(a,b) returns false because in b 132 is not the square of any number of a.
  21.  
  22. a = [121, 144, 19, 161, 19, 144, 19, 11]
  23. b = [121, 14641, 20736, 36100, 25921, 361, 20736, 361]
  24.  
  25. comp(a,b) returns false because in b 36100 is not the square of any number of a.
  26. Remarks
  27.  
  28. a or b might be [] (all languages). a or b might be nil or null or None (except in Haskell, Elixir, C++).
  29.  
  30. If a or b are nil (or null or None), the problem doesn't make sense so return false.
  31.  
  32. If a or b are empty the result is evident by itself.
На втором тестировании я не прохожу...
Листинг программы
  1. Test2
  2. Expected: False
  3. But was: True
  4. at AreTheySameTests.Test2 () <0x415a52a0 + 0x000fc> in :0

Все остальные тесты проходят успешно, что не так с моим кодом,где ошибка?

Я даже добавил этот вариант, на всякий случай:
Листинг программы
  1. bool check = a.Where(i => !b.Contains(i * i)).Count() > 0 ? false : true;
  2. if (!check)
  3. check = b.Where(i => !a.Contains(i * i)).Count() > 0 ? false : true;
  4. return check;
Было просто:
Листинг программы
  1. return a.Where(i => !b.Contains(i * i)).Count() > 0 ? false : true;

Решение задачи: «Сравнение двух массивов»

textual
Листинг программы
  1. bool check = b.All(i => a.Contains((int)Math.Sqrt(b));

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

10   голосов , оценка 3.9 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы