Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1 zadacha /////////////// Problem 1. Fibonacci Numbers
- static int Fibonacci(int count)
- {
- int firstNumber = 0;
- int secondNumber = 1;
- int result = 0;
- if (count == 0)
- {
- Console.WriteLine(secondNumber);
- }
- else
- {
- for (int i = 0; i < count; i++)
- {
- result = firstNumber + secondNumber;
- firstNumber = secondNumber;
- secondNumber = result;
- }
- }
- return result;
- }
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- Console.WriteLine(Fibonacci(n));
- }
- 2 zadacha //////////////////// Problem 2. Prime Checker
- static bool PrimeChecker(long input)
- {
- if (input < 2)
- {
- return false;
- }
- else
- {
- int divider = 2;
- double maxDivider = Math.Sqrt(input);
- while (divider <= maxDivider)
- {
- if (input % divider == 0)
- {
- return false;
- }
- divider++;
- }
- }
- return true;
- }
- static void Main()
- {
- long number = long.Parse(Console.ReadLine());
- Console.WriteLine("\"{0}\" is prime? {1}", number, PrimeChecker(number));
- }
- 3 zadacha ////////////////////// Problem 3. Primes in Given Range
- static List<int> FindPrimes(int startNum, int endNum)
- {
- List<int> primesCollection = new List<int>();
- if (startNum < 2)
- {
- startNum = 2;
- }
- for (int i = startNum; i <= endNum; i++)
- {
- int divider = 2;
- double maxDivider = Math.Sqrt(i);
- bool isPrime = true;
- while (divider <= maxDivider)
- {
- if (i % divider == 0)
- {
- isPrime = false;
- break;
- }
- divider++;
- }
- if (isPrime)
- {
- primesCollection.Add(i);
- }
- }
- return primesCollection;
- }
- static void Main()
- {
- int startNum = int.Parse(Console.ReadLine());
- int endNum = int.Parse(Console.ReadLine());
- if (startNum > endNum)
- {
- Console.WriteLine("(empty list)");
- }
- else
- {
- List<int> primes = FindPrimes(startNum, endNum);
- for (int i = 0; i < primes.Count; i++)
- {
- if (i < primes.Count - 1)
- {
- Console.Write(primes[i] + ", ");
- }
- else
- {
- Console.WriteLine(primes[i]);
- }
- 4 zadacha ////////////////////////////////// Problem 4. Difference between Dates
- static TimeSpan DaysDifference(DateTime startDate, DateTime endDate)
- {
- TimeSpan days = endDate - startDate;
- return days;
- }
- static void Main()
- {
- CultureInfo bgBG = new CultureInfo("bg-BG");
- try
- {
- DateTime startDate1 = DateTime.ParseExact(Console.ReadLine(), "d.MM.yyyy", bgBG);
- DateTime endDate1 = DateTime.ParseExact(Console.ReadLine(), "d.MM.yyyy", bgBG);
- Console.WriteLine(DaysDifference(startDate1, endDate1).TotalDays);
- }
- catch (FormatException)
- {
- Console.WriteLine("Invalid date! Dates must be in format dd.MM.yyyy.");
- }
- 5 zadacha ////////////////////////////////////// Problem 5. Sorting Numbers
- static int[] FillTheArray(int inputCount)
- {
- int[] array = new int[inputCount];
- for (int i = 0; i < array.Length; i++)
- {
- array[i] = int.Parse(Console.ReadLine());
- }
- Array.Sort(array);
- return array;
- }
- static void Main()
- {
- int numbersCount = int.Parse(Console.ReadLine());
- int[] numbersArray = FillTheArray(numbersCount);
- Console.WriteLine();
- foreach (var number in numbersArray)
- {
- Console.WriteLine(number);
- }
- 6 zadacha /////////////////////////////////// Problem 6. Longest Area in Array
- static void Main()
- {
- int arrayLenght = int.Parse(Console.ReadLine());
- string[] words = new string[arrayLenght];
- for (int i = 0; i < words.Length; i++)
- {
- string arrayElement = Console.ReadLine();
- words[i] = arrayElement;
- }
- int startIndex = 0;
- int lenghtCount = 1;
- int currentCount = 1;
- for (int i = 0; i < words.Length - 1; i++) //could start on index 1 and check current with previous elements
- {
- if (words[i] == words[i + 1])
- {
- currentCount++;
- if (currentCount > lenghtCount)
- {
- lenghtCount = currentCount;
- startIndex = (i + 1) - (lenghtCount - 1);
- }
- }
- else
- {
- currentCount = 1;
- }
- }
- Console.WriteLine(lenghtCount);
- for (int i = 0; i < lenghtCount; i++)
- {
- Console.WriteLine(words[startIndex + i]);
- }
- 7 zadacha /////////////////////////////////////////////////////// Problem 7. Matrix of Palindromes
- int matrixRows = int.Parse(Console.ReadLine());
- int matrixCols = int.Parse(Console.ReadLine());
- string[,] polidromesMatrix = new string[matrixRows, matrixCols];
- for (int row = 0; row < matrixRows; row++)
- {
- for (int col = 0; col < matrixCols; col++)
- {
- polidromesMatrix[row, col] = "" + (char)('a' + row) + (char)('a' + row + col) + (char)('a' + row);
- }
- }
- for (int row = 0; row < matrixRows; row++)
- {
- for (int col = 0; col < matrixCols; col++)
- {
- Console.Write(polidromesMatrix[row, col] + " ");
- }
- Console.WriteLine();
- }
- 8 zadacha ///////////////// Problem 8. * Longest Non-Decreasing Subsequence
- //Input
- string input = Console.ReadLine();
- //Split the elements into an array
- string[] inputElements = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- //Fill an integer array from string array
- int[] elementsArray = new int[inputElements.Length];
- for (int i = 0; i < inputElements.Length; i++)
- {
- elementsArray[i] = int.Parse(inputElements[i]);
- }
- List<int> longestSequenceList = new List<int>();
- //Find the longest sequence of identical elements
- int startIndex = 0;
- int lenghtCount = 1;
- int currentCount = 1;
- for (int i = 0; i < elementsArray.Length - 1; i++) //could start on index 1 and check current with previous elements
- {
- if (elementsArray[i] == elementsArray[i + 1])
- {
- currentCount++;
- if (currentCount > lenghtCount)
- {
- lenghtCount = currentCount;
- startIndex = (i + 1) - (lenghtCount - 1);
- }
- }
- else
- {
- currentCount = 1;
- }
- }
- //Make the sequence currently longest
- for (int i = 0; i < lenghtCount; i++)
- {
- longestSequenceList.Add(elementsArray[startIndex + i]);
- }
- //Find how many combinations of sequences can be there
- long combinations = 1;
- for (int i = 0; i < elementsArray.Length; i++) //Instead of Math.Pow
- {
- combinations *= 2;
- }
- for (long combination = 1; combination <= combinations; combination++)
- {
- //convert current combination number to its binary representation.
- //That way we will use the positions with bit "1" with the elements on the same position in the array
- string binary = Convert.ToString(combination, 2).PadLeft(elementsArray.Length, '0');
- char[] tempArr = binary.ToCharArray();
- Array.Reverse(tempArr);
- string revBinary = new string(tempArr);
- List<int> tempList = new List<int>();
- int bitsCount = 0;
- for (int i = 0; i < elementsArray.Length; i++)
- {
- if (revBinary[i] == '1')
- {
- tempList.Add(elementsArray[i]);
- bitsCount++;
- }
- }
- if (bitsCount < longestSequenceList.Count) //Speed optimisation
- {
- continue;
- }
- int currentLenght = 0;
- List<int> currentLongestSeq = new List<int>();
- if (tempList.Count > 1) //Avoid cases where the current combination will use only 1 element
- {
- int biggestNum = tempList[0];
- currentLongestSeq.Add(biggestNum);
- for (int i = 0; i < tempList.Count - 1; i++)
- {
- //If the next number in the current combination sequence is bigger add it to final list
- if (tempList[i + 1] > biggestNum)
- {
- biggestNum = tempList[i + 1];
- currentLongestSeq.Add(biggestNum);
- }
- }
- currentLenght = currentLongestSeq.Count;
- }
- if (currentLenght > longestSequenceList.Count)
- {
- longestSequenceList = currentLongestSeq;
- }
- }
- //Output
- for (int i = 0; i < longestSequenceList.Count; i++)
- {
- Console.Write(longestSequenceList[i] + " ");
- }
- Console.WriteLine();
- 9 zadacha ///////////////////////////////// Problem 9. Remove Names
- string firstInputLine = Console.ReadLine();
- string[] firstNames = firstInputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- string secondInputLine = Console.ReadLine();
- string[] secondNames = secondInputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- List<string> firstLine = firstNames.ToList<string>();
- List<string> secondLine = secondNames.ToList<string>();
- for (int i = 0; i < secondLine.Count; i++)
- {
- for (int j = 0; j < firstLine.Count; j++)
- {
- if (firstLine.Contains(secondLine[i]))
- {
- firstLine.Remove(secondLine[i]);
- }
- }
- }
- foreach (var item in firstLine)
- {
- Console.Write(item + " ");
- }
- Console.WriteLine();
- 10 zadacha //////////// Problem 10. Join Lists
- string firstLine = Console.ReadLine();
- string secondLine = Console.ReadLine();
- string[] firstLineNums = firstLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- string[] secondLineNums = secondLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- List<int> mainList = new List<int>();
- List<int> auxiliaryList = new List<int>();
- for (int i = 0; i < firstLineNums.Length; i++)
- {
- auxiliaryList.Add(int.Parse(firstLineNums[i]));
- }
- for (int i = 0; i < secondLineNums.Length; i++)
- {
- auxiliaryList.Add(int.Parse(secondLineNums[i]));
- }
- auxiliaryList.Sort();
- mainList = auxiliaryList.Distinct().ToList(); //LINQ - Distinct erases the duplicated elements
- foreach (int number in mainList)
- {
- Console.Write(number + " ");
- }
- Console.WriteLine();
- 11 zadacha ////////////// Problem 11. Count of Letters
- string lettersString = Console.ReadLine();
- string[] allLetter = lettersString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- string allLetters = string.Join("", allLetter);
- List<char> letters = new List<char>();
- for (int i = 0; i < allLetters.Length; i++)
- {
- letters.Add(Convert.ToChar(allLetters[i]));
- }
- letters.Sort();
- int counter = 1;
- for (int i = 1; i < letters.Count; i++)
- {
- if (letters[i] == letters[i - 1])
- {
- counter++;
- }
- else
- {
- Console.WriteLine("{0} --> {1}", letters[i - 1], counter ,counter,new string('#', counter));
- counter = 1;
- }
- if (i == letters.Count - 1)
- {
- Console.WriteLine("{0} --> {1}", letters[i], counter);
- }
- }
- 12 zadacha /////////////////////// Problem 12. Count of Names
- //Same as last task, just change the list to <string>
- string namesString = Console.ReadLine();
- string[] allNames = namesString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- List<string> letters = allNames.ToList<string>();
- letters.Sort();
- int counter = 1;
- for (int i = 1; i < letters.Count; i++)
- {
- if (letters[i] == letters[i - 1])
- {
- counter++;
- }
- else
- {
- Console.WriteLine("{0} --> {1}", letters[i - 1], counter);
- counter = 1;
- }
- if (i == letters.Count - 1)
- {
- Console.WriteLine("{0} --> {1}", letters[i], counter);
- }
- 13 zadacha ////////// Problem 13. Average Load Time Calculator
- 14 zadacga //////////// Problem 14. Longest Word in a Text
- string someText = Console.ReadLine();
- string[] allWords = someText.Split(new char[] { ' ', ',', ':', ';', '.' },
- StringSplitOptions.RemoveEmptyEntries);
- int longestWordIndex = 0;
- for (int i = 1; i < allWords.Length; i++)
- {
- if (allWords[i].Length > allWords[longestWordIndex].Length)
- {
- longestWordIndex = i;
- }
- }
- Console.WriteLine(allWords[longestWordIndex]);
- 15 zadacha ///////////////
- 16 zadacha ////////////// Problem 16. * Counting a Word in a Text
- static int CompareWords(string inputWord, string inputText)
- {
- string[] allWords = inputText.Split(
- new char[] { ' ', '.', ',', '"', '@', '!', '?', '/', '\\', ':', ';', '(', ')' },
- StringSplitOptions.None);
- int counter = 0;
- for (int i = 0; i < allWords.Length; i++)
- {
- //string.Equals give better options to compare strings (like ignore casing)
- if (string.Equals(allWords[i], inputWord, StringComparison.OrdinalIgnoreCase))
- {
- counter++;
- }
- }
- return counter;
- }
- static void Main()
- {
- string keyWord = Console.ReadLine();
- string someText = Console.ReadLine();
- int result = CompareWords(keyWord, someText);
- Console.WriteLine("{0} times :{1}",keyWord ,result);
- 17 zadacha /////////////Perimeter and Area of Polygon
- class Point
- {
- public double x;
- public double y;
- public Point(double X, double Y)
- {
- x = X;
- y = Y;
- }
- public double Distance(Point p2)
- {
- double x1 = this.x;
- double y1 = this.y;
- double x2 = p2.x;
- double y2 = p2.y;
- return Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2));
- }
- }
- class Polygon
- {
- List<Point> poly;
- int m;
- public Polygon(List<Point> polygon)
- {
- if (polygon.Count > 2)
- {
- this.poly = polygon;
- poly.Add(polygon[0]);
- m = poly.Count();
- }
- else
- {
- Console.WriteLine("Cannot make polygon with less than 3 points!");
- }
- }
- public double Perimeter()
- {
- double perimeter = 0;
- for (int i = 0; i < m-1; i++)
- {
- Point p1 = poly[i];
- Point p2 = poly[i + 1];
- perimeter += p1.Distance(p2);
- }
- return perimeter;
- }
- public double Area()
- {
- double left = 0;
- double right = 0;
- for (int i = 0; i < m-1; i++)
- {
- Point p1 = poly[i];
- Point p2 = poly[i+1];
- left += p1.x * p2.y;
- right += p2.x * p1.y;
- }
- return Math.Abs(left-right)/2;
- }
- }
- public class Program
- {
- public static void Main()
- {
- Console.WriteLine(@"Program that calculates the perimeter and the area of given polygon (not necessarily convex)
- consisting of n floating-point coordinates in the 2D plane.");
- Console.WriteLine("Write n = ");
- int n = int.Parse(Console.ReadLine());
- List<Point> listOfPoints = new List<Point>{};
- for (int i = 0; i < n; i++)
- {
- Console.WriteLine("Write coordinates of point {0}:", i + 1);
- string line = Console.ReadLine();
- string[] list = line.Split(' ');
- double x = double.Parse(list[0]);
- double y = double.Parse(list[1]);
- Point p = new Point(x, y);
- listOfPoints.Add(p);
- }
- Polygon polygon = new Polygon(listOfPoints);
- double perimeter = polygon.Perimeter();
- double area = polygon.Area();
- Console.WriteLine("The area is {0:0.00} and the perimeter is {1:0.00}.", area, perimeter);
- }
Advertisement
Add Comment
Please, Sign In to add comment