Advertisement
dimipan80

Exam 9. Sequence of K Numbers

Jun 23rd, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. namespace _2.SequenceOfK_Numbers
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.  
  6.     public class SequenceOfK_Numbers
  7.     {
  8.         public static void Main(string[] args)
  9.         {
  10.             checked
  11.             {
  12.                 string inputLine = Console.ReadLine();
  13.                 int numK = int.Parse(Console.ReadLine());
  14.  
  15.                 if (numK > 1)
  16.                 {
  17.                     int[] seqNums = SplitInputStringAndCreateArrayOfNumbers(inputLine);
  18.                     int countOfEquals = 1;
  19.                     for (int i = 1; i < seqNums.Length; i++)
  20.                     {
  21.                         int previousNum = seqNums[i - 1];
  22.                         if (i < seqNums.Length - 1)
  23.                         {
  24.                             if (seqNums[i] == previousNum)
  25.                             {
  26.                                 countOfEquals++;
  27.                             }
  28.                             else
  29.                             {
  30.                                 PrintNextRightNumber(numK, countOfEquals, previousNum);
  31.                                 countOfEquals = 1;
  32.                             }
  33.                         }
  34.                         else
  35.                         {
  36.                             if (seqNums[i] == previousNum)
  37.                             {
  38.                                 countOfEquals++;
  39.                                 PrintNextRightNumber(numK, countOfEquals, previousNum);
  40.                             }
  41.                             else
  42.                             {
  43.                                 Console.WriteLine(seqNums[i]);
  44.                             }                                                
  45.                         }
  46.                     }
  47.                 }
  48.             }
  49.         }
  50.  
  51.         private static void PrintNextRightNumber(int numK, int countOfEquals, int previousNum)
  52.         {
  53.             checked
  54.             {
  55.                 int maxWrite = countOfEquals % numK;
  56.                 for (int j = 0; j < maxWrite; j++)
  57.                 {
  58.                     Console.Write("{0} ", previousNum);
  59.                 }
  60.             }
  61.         }
  62.  
  63.         private static int[] SplitInputStringAndCreateArrayOfNumbers(string inputStr)
  64.         {
  65.             checked
  66.             {                
  67.                 char[] separators = new char[] { ' ', ',', ';' };
  68.                 string[] numStr = inputStr.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  69.                 int[] nums = new int[numStr.Length];
  70.                 for (int i = 0; i < numStr.Length; i++)
  71.                 {
  72.                     nums[i] = int.Parse(numStr[i]);
  73.                 }
  74.  
  75.                 return nums;
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement