fork(1) download
  1.  
  2.  
  3. using System;
  4.  
  5. namespace For39262
  6. {
  7. internal class Program
  8. {
  9. private static void Main(string[] args)
  10. {
  11. Console.WriteLine("Here we go...");
  12.  
  13. int[] A = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
  14. int K = 3;
  15.  
  16. TaskNo1 taskNo1 = new TaskNo1(A, K);
  17.  
  18. try
  19. {
  20. int[] result = taskNo1.GetResult();
  21. for (int i = 0; i < result.Length; i++)
  22. {
  23. Console.WriteLine(result[i]);
  24. }
  25. Console.WriteLine("Extracted elements: "
  26. + result.Length + "/" + A.Length);
  27. }
  28. catch (TaskException ex)
  29. {
  30. Console.WriteLine(ex.Message);
  31. }
  32.  
  33. Console.Read();
  34. }
  35.  
  36. public class TaskNo1
  37. {
  38. public readonly int[] A;
  39. public readonly int N;
  40. public readonly int K;
  41.  
  42. public TaskNo1(int[] a, int k)
  43. {
  44. A = a;
  45. K = k;
  46. N = A.Length;
  47. }
  48.  
  49. public int[] GetResult()
  50. {
  51. // Проверка уловия для K
  52. if (!IsInitialСonditionsSatisfied())
  53. throw new TaskException("K is wrong");
  54.  
  55. // Определение длины массива для результата
  56. int lengthOfResult = 0;
  57. for (int i = 1; i <= N; i++)
  58. {
  59. if (isAmultipleFor(i, K))
  60. {
  61. lengthOfResult++;
  62. }
  63. }
  64.  
  65. int[] result = new int[lengthOfResult];
  66.  
  67. // Заполнение массива резульатами
  68. int resultCounter = 0;
  69. for (int i = 1; i <= N; i++)
  70. {
  71. if (isAmultipleFor(i, K))
  72. {
  73. int realIndex = i - 1;
  74. result[resultCounter] = A[realIndex];
  75. resultCounter++;
  76. }
  77. }
  78.  
  79. return result;
  80. }
  81.  
  82. public bool IsInitialСonditionsSatisfied()
  83. {
  84. if (K < 1)
  85. return false;
  86.  
  87. if (K > N)
  88. return false;
  89.  
  90. return true;
  91. }
  92.  
  93. public bool isAmultipleFor(int a, int b)
  94. {
  95. return a % b == 0;
  96. }
  97. }
  98.  
  99. internal class TaskException : ApplicationException
  100. {
  101. public TaskException(string message)
  102. : base(message)
  103. {
  104. }
  105. }
  106. }
  107. }
  108.  
  109.  
Success #stdin #stdout 0.04s 33912KB
stdin
Standard input is empty
stdout
Here we go...
3
6
9
12
Extracted elements: 4/12