Advertisement
Guest User

List of Predicates

a guest
Jun 10th, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _09.ListOfPredicates
  6. {
  7.     class ListOfPredicates
  8.     {
  9.         static void Main()
  10.         {
  11.             int range = int.Parse(Console.ReadLine());
  12.             int[] divisors = Console.ReadLine().Split().Select(int.Parse).ToArray();
  13.             int[] numbers = new int[range];
  14.             for (int i = 1; i <= range; i++)
  15.             {
  16.                 numbers[i - 1] = i;
  17.             }
  18.  
  19.             divisors = divisors.Distinct().ToArray();
  20.  
  21.             List<int> divisibleNumbers = GetDivisible(numbers, divisors, (x, y) => x % y == 0);
  22.             Console.WriteLine(string.Join(" ", divisibleNumbers));
  23.         }
  24.  
  25.         private static List<int> GetDivisible(int[] numbers, int[] divisors, Func<int, int, bool> pred)
  26.         {
  27.             List<int> newNumbers = new List<int>();
  28.             bool isDivisible;
  29.             for (int i = 0; i < numbers.Length; i++)
  30.             {
  31.                 isDivisible = true;
  32.                 for (int j = 0; j < divisors.Length; j++)
  33.                 {
  34.                     if (!pred(numbers[i], divisors[j]))
  35.                     {
  36.                         isDivisible = false;
  37.                         break;
  38.                     }
  39.                 }
  40.  
  41.                 if (isDivisible)
  42.                 {
  43.                     newNumbers.Add(numbers[i]);
  44.                 }
  45.             }
  46.  
  47.             return newNumbers;
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement