Advertisement
n4wn4w

C# homework ADVANCED

Apr 16th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.11 KB | None | 0 0
  1.  
  2. 1 zadacha ///////////////  Problem 1.   Fibonacci Numbers
  3.  
  4. static int Fibonacci(int count)
  5.     {
  6.         int firstNumber = 0;
  7.         int secondNumber = 1;
  8.         int result = 0;
  9.  
  10.         if (count == 0)
  11.         {
  12.             Console.WriteLine(secondNumber);
  13.         }
  14.         else
  15.         {
  16.             for (int i = 0; i < count; i++)
  17.             {
  18.                 result = firstNumber + secondNumber;
  19.                 firstNumber = secondNumber;
  20.                 secondNumber = result;
  21.             }
  22.         }
  23.  
  24.         return result;
  25.     }
  26.  
  27.     static void Main()
  28.     {
  29.         int n = int.Parse(Console.ReadLine());
  30.  
  31.         Console.WriteLine(Fibonacci(n));
  32.     }
  33.  
  34.  
  35. 2 zadacha ////////////////////   Problem 2. Prime Checker
  36.  
  37.  static bool PrimeChecker(long input)
  38.         {
  39.             if (input < 2)
  40.             {
  41.                 return false;
  42.             }
  43.             else
  44.             {
  45.                 int divider = 2;
  46.                 double maxDivider = Math.Sqrt(input);
  47.  
  48.                 while (divider <= maxDivider)
  49.                 {
  50.                     if (input % divider == 0)
  51.                     {
  52.                         return false;
  53.                     }
  54.  
  55.                     divider++;
  56.                 }
  57.             }
  58.  
  59.             return true;
  60.         }
  61.  
  62.         static void Main()
  63.         {
  64.             long number = long.Parse(Console.ReadLine());
  65.  
  66.             Console.WriteLine("\"{0}\" is prime? {1}", number, PrimeChecker(number));
  67.         }
  68.  
  69.  
  70. 3 zadacha //////////////////////   Problem 3.   Primes in Given Range
  71.  
  72. static List<int> FindPrimes(int startNum, int endNum)
  73.     {
  74.         List<int> primesCollection = new List<int>();
  75.  
  76.         if (startNum < 2)
  77.         {
  78.             startNum = 2;
  79.         }
  80.  
  81.         for (int i = startNum; i <= endNum; i++)
  82.         {
  83.             int divider = 2;
  84.             double maxDivider = Math.Sqrt(i);
  85.             bool isPrime = true;
  86.  
  87.             while (divider <= maxDivider)
  88.             {
  89.                 if (i % divider == 0)
  90.                 {
  91.                     isPrime = false;
  92.                     break;
  93.                 }
  94.  
  95.                 divider++;
  96.             }
  97.  
  98.             if (isPrime)
  99.             {
  100.                 primesCollection.Add(i);
  101.             }
  102.         }
  103.  
  104.         return primesCollection;
  105.     }
  106.  
  107.     static void Main()
  108.     {
  109.         int startNum = int.Parse(Console.ReadLine());
  110.         int endNum = int.Parse(Console.ReadLine());
  111.  
  112.         if (startNum > endNum)
  113.         {
  114.             Console.WriteLine("(empty list)");
  115.         }
  116.         else
  117.         {
  118.             List<int> primes = FindPrimes(startNum, endNum);
  119.  
  120.             for (int i = 0; i < primes.Count; i++)
  121.             {
  122.                 if (i < primes.Count - 1)
  123.                 {
  124.                     Console.Write(primes[i] + ", ");
  125.                 }
  126.                 else
  127.                 {
  128.                     Console.WriteLine(primes[i]);
  129.                 }
  130.  
  131.  
  132.  
  133. 4 zadacha //////////////////////////////////  Problem 4.    Difference between Dates
  134.  
  135. static TimeSpan DaysDifference(DateTime startDate, DateTime endDate)
  136.     {
  137.         TimeSpan days = endDate - startDate;
  138.  
  139.         return days;
  140.     }
  141.  
  142.     static void Main()
  143.     {
  144.         CultureInfo bgBG = new CultureInfo("bg-BG");
  145.  
  146.         try
  147.         {
  148.             DateTime startDate1 = DateTime.ParseExact(Console.ReadLine(), "d.MM.yyyy", bgBG);
  149.             DateTime endDate1 = DateTime.ParseExact(Console.ReadLine(), "d.MM.yyyy", bgBG);
  150.  
  151.             Console.WriteLine(DaysDifference(startDate1, endDate1).TotalDays);
  152.         }
  153.         catch (FormatException)
  154.         {
  155.             Console.WriteLine("Invalid date! Dates must be in format dd.MM.yyyy.");
  156.         }
  157.  
  158.  
  159. 5 zadacha //////////////////////////////////////    Problem 5.  Sorting Numbers
  160.  
  161.  static int[] FillTheArray(int inputCount)
  162.         {
  163.             int[] array = new int[inputCount];
  164.  
  165.             for (int i = 0; i < array.Length; i++)
  166.             {
  167.                 array[i] = int.Parse(Console.ReadLine());
  168.             }
  169.  
  170.             Array.Sort(array);
  171.             return array;
  172.         }
  173.  
  174.         static void Main()
  175.         {
  176.             int numbersCount = int.Parse(Console.ReadLine());
  177.             int[] numbersArray = FillTheArray(numbersCount);
  178.  
  179.             Console.WriteLine();
  180.  
  181.             foreach (var number in numbersArray)
  182.             {
  183.                 Console.WriteLine(number);
  184.             }
  185.  
  186.  
  187. 6 zadacha ///////////////////////////////////     Problem 6.    Longest Area in Array
  188.  
  189.         static void Main()
  190.         {
  191.             int arrayLenght = int.Parse(Console.ReadLine());
  192.             string[] words = new string[arrayLenght];
  193.  
  194.             for (int i = 0; i < words.Length; i++)
  195.             {
  196.                 string arrayElement = Console.ReadLine();
  197.                 words[i] = arrayElement;
  198.             }
  199.  
  200.             int startIndex = 0;
  201.             int lenghtCount = 1;
  202.             int currentCount = 1;
  203.  
  204.             for (int i = 0; i < words.Length - 1; i++) //could start on index 1 and check current with previous elements
  205.             {
  206.                 if (words[i] == words[i + 1])
  207.                 {
  208.                     currentCount++;
  209.  
  210.                     if (currentCount > lenghtCount)
  211.                     {
  212.                         lenghtCount = currentCount;
  213.                         startIndex = (i + 1) - (lenghtCount - 1);
  214.                     }
  215.                 }
  216.                 else
  217.                 {
  218.                     currentCount = 1;
  219.                 }
  220.             }
  221.  
  222.             Console.WriteLine(lenghtCount);
  223.             for (int i = 0; i < lenghtCount; i++)
  224.             {
  225.                 Console.WriteLine(words[startIndex + i]);
  226.             }
  227.  
  228. 7 zadacha ///////////////////////////////////////////////////////    Problem 7. Matrix of Palindromes
  229.  
  230.  int matrixRows = int.Parse(Console.ReadLine());
  231.         int matrixCols = int.Parse(Console.ReadLine());
  232.  
  233.         string[,] polidromesMatrix = new string[matrixRows, matrixCols];
  234.  
  235.         for (int row = 0; row < matrixRows; row++)
  236.         {
  237.             for (int col = 0; col < matrixCols; col++)
  238.             {
  239.                 polidromesMatrix[row, col] = "" + (char)('a' + row) + (char)('a' + row + col) + (char)('a' + row);
  240.  
  241.                
  242.             }
  243.         }
  244.  
  245.         for (int row = 0; row < matrixRows; row++)
  246.         {
  247.             for (int col = 0; col < matrixCols; col++)
  248.             {
  249.                 Console.Write(polidromesMatrix[row, col] + " ");
  250.             }
  251.             Console.WriteLine();
  252.         }
  253.  
  254.  
  255. 8 zadacha /////////////////   Problem 8.    * Longest Non-Decreasing Subsequence
  256.  
  257. //Input
  258.             string input = Console.ReadLine();
  259.             //Split the elements into an array
  260.             string[] inputElements = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  261.  
  262.             //Fill an integer array from string array
  263.             int[] elementsArray = new int[inputElements.Length];
  264.             for (int i = 0; i < inputElements.Length; i++)
  265.             {
  266.                 elementsArray[i] = int.Parse(inputElements[i]);
  267.             }
  268.  
  269.             List<int> longestSequenceList = new List<int>();
  270.  
  271.             //Find the longest sequence of identical elements
  272.             int startIndex = 0;
  273.             int lenghtCount = 1;
  274.             int currentCount = 1;
  275.  
  276.             for (int i = 0; i < elementsArray.Length - 1; i++) //could start on index 1 and check current with previous elements
  277.             {
  278.                 if (elementsArray[i] == elementsArray[i + 1])
  279.                 {
  280.                     currentCount++;
  281.  
  282.                     if (currentCount > lenghtCount)
  283.                     {
  284.                         lenghtCount = currentCount;
  285.                         startIndex = (i + 1) - (lenghtCount - 1);
  286.                     }
  287.                 }
  288.                 else
  289.                 {
  290.                     currentCount = 1;
  291.                 }
  292.             }
  293.  
  294.             //Make the sequence currently longest
  295.             for (int i = 0; i < lenghtCount; i++)
  296.             {
  297.                 longestSequenceList.Add(elementsArray[startIndex + i]);
  298.             }
  299.  
  300.             //Find how many combinations of sequences can be there
  301.             long combinations = 1;
  302.             for (int i = 0; i < elementsArray.Length; i++) //Instead of Math.Pow
  303.             {
  304.                 combinations *= 2;
  305.             }
  306.  
  307.             for (long combination = 1; combination <= combinations; combination++)
  308.             {
  309.                 //convert current combination number to its binary representation.
  310.                 //That way we will use the positions with bit "1" with the elements on the same position in the array
  311.                 string binary = Convert.ToString(combination, 2).PadLeft(elementsArray.Length, '0');
  312.                 char[] tempArr = binary.ToCharArray();
  313.                 Array.Reverse(tempArr);
  314.                 string revBinary = new string(tempArr);
  315.  
  316.                 List<int> tempList = new List<int>();
  317.                 int bitsCount = 0;
  318.  
  319.                 for (int i = 0; i < elementsArray.Length; i++)
  320.                 {
  321.                     if (revBinary[i] == '1')
  322.                     {
  323.                         tempList.Add(elementsArray[i]);
  324.                         bitsCount++;
  325.                     }
  326.                 }
  327.  
  328.                 if (bitsCount < longestSequenceList.Count) //Speed optimisation
  329.                 {
  330.                     continue;
  331.                 }
  332.  
  333.                 int currentLenght = 0;
  334.                 List<int> currentLongestSeq = new List<int>();
  335.  
  336.                 if (tempList.Count > 1) //Avoid cases where the current combination will use only 1 element
  337.                 {
  338.                     int biggestNum = tempList[0];
  339.                     currentLongestSeq.Add(biggestNum);
  340.  
  341.                     for (int i = 0; i < tempList.Count - 1; i++)
  342.                     {
  343.                         //If the next number in the current combination sequence is bigger add it to final list
  344.                         if (tempList[i + 1] > biggestNum)
  345.                         {
  346.                             biggestNum = tempList[i + 1];
  347.  
  348.                             currentLongestSeq.Add(biggestNum);
  349.                         }
  350.                     }
  351.  
  352.                     currentLenght = currentLongestSeq.Count;
  353.                 }
  354.  
  355.                 if (currentLenght > longestSequenceList.Count)
  356.                 {
  357.                     longestSequenceList = currentLongestSeq;
  358.                 }
  359.             }
  360.  
  361.             //Output
  362.             for (int i = 0; i < longestSequenceList.Count; i++)
  363.             {
  364.                 Console.Write(longestSequenceList[i] + " ");
  365.             }
  366.             Console.WriteLine();
  367.  
  368. 9 zadacha /////////////////////////////////  Problem 9. Remove Names
  369.  
  370. string firstInputLine = Console.ReadLine();
  371.             string[] firstNames = firstInputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  372.             string secondInputLine = Console.ReadLine();
  373.             string[] secondNames = secondInputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  374.  
  375.             List<string> firstLine = firstNames.ToList<string>();
  376.             List<string> secondLine = secondNames.ToList<string>();
  377.  
  378.             for (int i = 0; i < secondLine.Count; i++)
  379.             {
  380.                 for (int j = 0; j < firstLine.Count; j++)
  381.                 {
  382.                     if (firstLine.Contains(secondLine[i]))
  383.                     {
  384.                         firstLine.Remove(secondLine[i]);
  385.                     }
  386.                 }
  387.             }
  388.  
  389.             foreach (var item in firstLine)
  390.             {
  391.                 Console.Write(item + " ");
  392.             }
  393.             Console.WriteLine();
  394.  
  395. 10 zadacha //////////// Problem 10. Join Lists
  396.  
  397. string firstLine = Console.ReadLine();
  398.             string secondLine = Console.ReadLine();
  399.             string[] firstLineNums = firstLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  400.             string[] secondLineNums = secondLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  401.  
  402.             List<int> mainList = new List<int>();
  403.             List<int> auxiliaryList = new List<int>();
  404.  
  405.             for (int i = 0; i < firstLineNums.Length; i++)
  406.             {
  407.                 auxiliaryList.Add(int.Parse(firstLineNums[i]));
  408.             }
  409.  
  410.             for (int i = 0; i < secondLineNums.Length; i++)
  411.             {
  412.                 auxiliaryList.Add(int.Parse(secondLineNums[i]));
  413.             }
  414.  
  415.             auxiliaryList.Sort();
  416.  
  417.             mainList = auxiliaryList.Distinct().ToList(); //LINQ - Distinct erases the duplicated elements
  418.  
  419.             foreach (int number in mainList)
  420.             {
  421.                 Console.Write(number + " ");
  422.             }
  423.             Console.WriteLine();
  424.  
  425. 11 zadacha //////////////  Problem 11.  Count of Letters
  426.  
  427.  
  428.         string lettersString = Console.ReadLine();
  429.         string[] allLetter = lettersString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  430.  
  431.         string allLetters = string.Join("", allLetter);
  432.  
  433.         List<char> letters = new List<char>();
  434.  
  435.         for (int i = 0; i < allLetters.Length; i++)
  436.         {
  437.             letters.Add(Convert.ToChar(allLetters[i]));
  438.         }
  439.  
  440.         letters.Sort();
  441.  
  442.         int counter = 1;
  443.         for (int i = 1; i < letters.Count; i++)
  444.         {
  445.             if (letters[i] == letters[i - 1])
  446.             {
  447.                 counter++;
  448.             }
  449.             else
  450.             {
  451.                 Console.WriteLine("{0} --> {1}", letters[i - 1], counter ,counter,new string('#', counter));
  452.                 counter = 1;
  453.             }
  454.  
  455.             if (i == letters.Count - 1)
  456.             {
  457.                 Console.WriteLine("{0} --> {1}", letters[i], counter);
  458.             }
  459.         }
  460.  
  461. 12 zadacha ///////////////////////      Problem 12. Count of Names
  462.  
  463.  
  464.  
  465. //Same as last task, just change the list to <string>
  466.         string namesString = Console.ReadLine();
  467.         string[] allNames = namesString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  468.  
  469.         List<string> letters = allNames.ToList<string>();
  470.  
  471.         letters.Sort();
  472.  
  473.         int counter = 1;
  474.         for (int i = 1; i < letters.Count; i++)
  475.         {
  476.             if (letters[i] == letters[i - 1])
  477.             {
  478.                 counter++;
  479.             }
  480.             else
  481.             {
  482.                 Console.WriteLine("{0} --> {1}", letters[i - 1], counter);
  483.                 counter = 1;
  484.             }
  485.  
  486.             if (i == letters.Count - 1)
  487.             {
  488.                 Console.WriteLine("{0} --> {1}", letters[i], counter);
  489.             }
  490.  
  491.  
  492. 13 zadacha //////////  Problem 13.  Average Load Time Calculator
  493.  
  494.  
  495. 14 zadacga //////////// Problem 14. Longest Word in a Text
  496.  
  497.             string someText = Console.ReadLine();
  498.             string[] allWords = someText.Split(new char[] { ' ', ',', ':', ';', '.' },
  499.                                                     StringSplitOptions.RemoveEmptyEntries);
  500.  
  501.             int longestWordIndex = 0;
  502.             for (int i = 1; i < allWords.Length; i++)
  503.             {
  504.                 if (allWords[i].Length > allWords[longestWordIndex].Length)
  505.                 {
  506.                     longestWordIndex = i;
  507.                 }
  508.             }
  509.  
  510.             Console.WriteLine(allWords[longestWordIndex]);
  511.  
  512.  
  513. 15 zadacha ///////////////  
  514.  
  515. 16 zadacha ////////////// Problem 16.   * Counting a Word in a Text
  516.  
  517. static int CompareWords(string inputWord, string inputText)
  518.     {
  519.         string[] allWords = inputText.Split(
  520.             new char[] { ' ', '.', ',', '"', '@', '!', '?', '/', '\\', ':', ';', '(', ')' },
  521.             StringSplitOptions.None);
  522.  
  523.         int counter = 0;
  524.  
  525.         for (int i = 0; i < allWords.Length; i++)
  526.         {
  527.             //string.Equals give better options to compare strings (like ignore casing)
  528.             if (string.Equals(allWords[i], inputWord, StringComparison.OrdinalIgnoreCase))
  529.             {
  530.                 counter++;
  531.             }
  532.         }
  533.         return counter;
  534.     }
  535.     static void Main()
  536.     {
  537.         string keyWord = Console.ReadLine();
  538.         string someText = Console.ReadLine();
  539.  
  540.         int result = CompareWords(keyWord, someText);
  541.         Console.WriteLine("{0} times :{1}",keyWord ,result);
  542.  
  543.  
  544. 17 zadacha /////////////Perimeter and Area of Polygon
  545.  
  546.  
  547. class Point
  548.     {
  549.         public double x;
  550.         public double y;
  551.  
  552.         public Point(double X, double Y)
  553.         {
  554.             x = X;
  555.             y = Y;
  556.         }
  557.  
  558.         public double Distance(Point p2)
  559.         {
  560.             double x1 = this.x;
  561.             double y1 = this.y;
  562.             double x2 = p2.x;
  563.             double y2 = p2.y;
  564.             return Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2));
  565.         }
  566.     }
  567.    
  568.     class Polygon
  569.     {
  570.         List<Point> poly;
  571.         int m;
  572.  
  573.         public Polygon(List<Point> polygon)
  574.         {
  575.             if (polygon.Count > 2)
  576.             {
  577.                 this.poly = polygon;
  578.                 poly.Add(polygon[0]);
  579.                 m = poly.Count();
  580.             }
  581.             else
  582.             {
  583.                 Console.WriteLine("Cannot make polygon with less than 3 points!");
  584.             }
  585.         }
  586.  
  587.         public double Perimeter()
  588.         {
  589.             double perimeter = 0;
  590.             for (int i = 0; i < m-1; i++)
  591.             {
  592.                 Point p1 = poly[i];
  593.                 Point p2 = poly[i + 1];
  594.                 perimeter += p1.Distance(p2);
  595.             }
  596.             return perimeter;
  597.         }
  598.  
  599.         public double Area()
  600.         {
  601.             double left = 0;
  602.             double right = 0;
  603.             for (int i = 0; i < m-1; i++)
  604.             {
  605.                 Point p1 = poly[i];
  606.                 Point p2 = poly[i+1];
  607.                 left += p1.x * p2.y;
  608.                 right += p2.x * p1.y;
  609.             }
  610.             return Math.Abs(left-right)/2;
  611.         }
  612.     }
  613.  
  614.     public class Program
  615.     {
  616.         public static void Main()
  617.         {
  618.             Console.WriteLine(@"Program that calculates the perimeter and the area of given polygon (not necessarily convex)
  619. consisting of n floating-point coordinates in the 2D plane.");
  620.             Console.WriteLine("Write n = ");
  621.             int n = int.Parse(Console.ReadLine());
  622.             List<Point> listOfPoints = new List<Point>{};
  623.             for (int i = 0; i < n; i++)
  624.             {
  625.                 Console.WriteLine("Write coordinates of point {0}:", i + 1);
  626.                 string line = Console.ReadLine();
  627.                 string[] list = line.Split(' ');
  628.                 double x = double.Parse(list[0]);
  629.                 double y = double.Parse(list[1]);
  630.                 Point p = new Point(x, y);
  631.                 listOfPoints.Add(p);
  632.             }
  633.             Polygon polygon = new Polygon(listOfPoints);
  634.             double perimeter = polygon.Perimeter();
  635.             double area = polygon.Area();
  636.             Console.WriteLine("The area is {0:0.00} and the perimeter is {1:0.00}.", area, perimeter);
  637.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement