Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- List<int> k = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 11, 10, 11, 12, 13 };
- List<int> kuk = new List<int>() { 1, 2, 3, 4, 5, 9, 13, 14, 15, 16, 16, 15 };
- List<int> firstList = new List<int>();
- firstList.AddRange(k);
- firstList.AddRange(kuk);
- firstList.Sort();
- Int32 index = 0;
- for (int i = 0; i < k.Count + kuk.Count - 1; i++)
- {
- if (firstList[index] == firstList[index + 1])
- firstList.RemoveAt(index);
- else
- {
- index++;
- }
- }
- for (int i = 0; i < firstList.Count; i++)
- {
- Console.WriteLine(firstList[i]);
- }
- 2 zadacha ////////////////////////////////////////////////////////////////////////////////
- static int[] numbers;
- //LINQ sorting
- static void LINQSort()
- {
- var newArray = from n in numbers
- orderby n
- select n;
- foreach (var num in newArray)
- {
- Console.Write(num+" ");
- }
- }
- // simple sorting algorithm
- static int[] SelectionSort()
- {
- for (int a = 0; a < numbers.Length - 1; a++)
- {
- int minValue = a;
- for (int b = a + 1; b < numbers.Length; b++)
- {
- if (numbers[b] < numbers[minValue])
- {
- minValue = b;
- }
- }
- int temp = numbers[a];
- numbers[a] = numbers[minValue];
- numbers[minValue] = temp;
- }
- return numbers;
- }
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- numbers = input.Split().Select(int.Parse).ToArray();
- Console.WriteLine("LINQ");
- LINQSort();
- Console.WriteLine();
- Console.WriteLine("Seltection Sort");
- SelectionSort();
- foreach (var num in numbers)
- {
- Console.Write(num+" ");
- }
- Console.WriteLine();
- 3 zadacha ////////////////////////////////////////////////////////////////////////////
- string input = Console.ReadLine();
- double[] numbers = input.Split().Select(double.Parse).ToArray();
- List<double> roundNum = new List<double>();
- List<double> zeroFractNum = new List<double>();
- for (int a = 0; a < numbers.Length; a++)
- {
- if (numbers[a] % 1 != 0)
- {
- zeroFractNum.Add(numbers[a]);
- }
- else
- {
- roundNum.Add(numbers[a]);
- }
- }
- Console.WriteLine("Non - Zero Fraction Numbers");
- zeroFractNum.ForEach(a => Console.Write(a + " "));
- Console.WriteLine("min -- {0}", zeroFractNum.Min());
- Console.WriteLine("max -- {0}", zeroFractNum.Max());
- Console.WriteLine("sum -- {0}", zeroFractNum.Sum());
- Console.WriteLine("average -- {0:f2}", zeroFractNum.Average());
- Console.WriteLine("Round Numbers");
- roundNum.ForEach(b => Console.Write(b + " "));
- Console.WriteLine("min -- {0}", roundNum.Min());
- Console.WriteLine("max -- {0}", roundNum.Max());
- Console.WriteLine("sum -- {0}", roundNum.Sum());
- Console.WriteLine("average -- {0:f2}", roundNum.Average());
- 4 zadacha ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- List<int> k = new List<int>() { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,
- 15,1,2,3,4,5,6,7,8,9,10,1,2,3
- ,4,5,6,7,8,9,0, };
- k.Sort();
- for (int i = 1; i < k.Count + 1; i++)
- {
- if (i == k.Count)
- {
- Console.Write(k[k.Count - 1] + " ");
- }
- if (i != k.Count)
- {
- if (k[i] == k[i - 1])
- {
- Console.Write(k[i] + " ");
- }
- else
- {
- Console.Write(k[i - 1] + " ");
- Console.WriteLine();
- }
- }
- }
- Console.WriteLine();
- ///////////////////////////////////// 2 reshenie ///////////////////////////////////////////////
- string[] input = Console.ReadLine().Split();
- for (int i = 0; i < input.Length - 1; i++)
- {
- Console.Write(input[i]+" ");
- if (!input[i].Equals(input[i + 1]))
- {
- Console.WriteLine();
- }
- }
- Console.Write(input[input.Length-1]);
- Console.WriteLine();
- 5 zadacha ////////////////////////////////////////////////////////////////////////////
- string input = Console.ReadLine();
- int[] numbers = input.Split().Select(int.Parse).ToArray();
- int counter = 1;
- int maxLength = 1;
- int end = 0;
- Console.Write(numbers[0]+" ");
- for (int i = 1; i < numbers.Length; i++)
- {
- if (numbers[i] > numbers[i - 1])
- {
- counter++;
- Console.Write(numbers[i]+" ");
- }
- else
- {
- counter = 1;
- Console.WriteLine();
- Console.Write(numbers[i]+" ");
- }
- if (counter > maxLength)
- {
- maxLength = counter;
- end = i;
- }
- }
- Console.WriteLine();
- Console.Write("Longest: ");
- for (int j = end - maxLength + 1; j <= end; j++)
- {
- Console.Write(numbers[j]+" ");
- }
- ///////////////// 7 zadacha ima e produljenie na 6 v neq ima sortirane an outputa i 2 resheniq ///////////////////////////
- static List<List<int>> subsets = new List<List<int>>();
- static int[] numbers;
- static int N;
- static bool solution = false;
- static void Main()
- {
- Console.Write("Please, enter a value for N: ");
- N = int.Parse(Console.ReadLine());
- Console.WriteLine("Please enter a sequence of numbers, separated by a space: ");
- numbers = Console.ReadLine().Split(' ').Select(int.Parse).Distinct().ToArray();
- Array.Sort(numbers);
- //numbers = new int[] { 1, 2, 3, 4 };
- Console.WriteLine("\nOutput:");
- List<int> subsetList = new List<int>();
- MakeSubset(0, subsetList);
- var sorted = subsets.OrderBy(x => x.Count);
- foreach (var item in sorted)
- {
- Console.WriteLine(" {0} = {1}", string.Join(" + ", item), N);
- }
- if (!solution)// if no sum matches N
- Console.WriteLine("No matching subsets.");
- }
- static void MakeSubset(int index, List<int> subset)
- {
- if (subset.Sum() == N && subset.Count > 0) // if subset sum = N, print it on the console
- {
- subsets.Add(new List<int>(subset));
- solution = true; // set solution to true, and we will not be printing that there is no solution
- }
- //Console.WriteLine(string.Join(" ", subset));
- for (int i = index; i < numbers.Length; i++)
- {
- subset.Add(numbers[i]);
- MakeSubset(i + 1, subset); // call MakeSubset recursively, every time starting from the previous index + 1
- subset.RemoveAt(subset.Count - 1); // remove last element
- }
- }
- ///////////////////////// 2 reshenie /////////////////////////////////////////////////////////////////
- static List<List<int>> subsets = new List<List<int>>();
- static int[] numbers;
- static int N;
- static bool solution = false;
- private static void PrintSubset(List<int> subset)
- {
- if (subset.Count == 1)
- {
- Console.WriteLine(subset[0]);
- }
- else
- {
- // print subset elements 0 to (subset.Count - 1), along with the operators
- for (int i = 0; i < subset.Count; i++)
- {
- if (i == 0)
- {
- Console.Write("{0} +", subset[i]);
- }
- else if (i > 0 && i < subset.Count - 1)
- {
- Console.Write(" {0} +", subset[i]);
- }
- else if (i == subset.Count - 1)
- {
- Console.Write(" {0} = {1}", subset[i], N);
- }
- }
- Console.WriteLine();
- }
- }
- private static int CalculateSum(List<int> subset)
- {
- int sum = 0;
- for (int i = 0; i < subset.Count; i++)
- sum += subset[i];
- return sum;
- }
- // when calling the method, we set the start index
- // this is why there is no value for index in the method itself
- static void MakeSubset(int index, List<int> subset)
- {
- int sum = CalculateSum(subset);
- if (sum == N) // if subset sum = N, store subset list in the subsets list of lists
- {
- subsets.Add(new List<int>(subset));
- solution = true; // set solution to true, and we will not be printing that there is no solution
- }
- if (subset.Count == numbers.Length) // if susbset size = input array size
- return; // there is nothing more to be done, return
- for (int i = index; i < numbers.Length; i++)
- {
- subset.Add(numbers[i]);
- MakeSubset(i + 1, subset); // call MakeSubset recursively, every time starting from the previous index + 1
- subset.RemoveAt(subset.Count - 1); // remove last element
- }
- }
- static void Main()
- {
- // input N
- Console.Write("Please, enter a value for N: ");
- N = int.Parse(Console.ReadLine());
- // input array
- Console.WriteLine("Please enter a sequence of numbers, separated by a space: ");
- numbers = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- Console.WriteLine("\nOutput:");
- // logic: make subsets, calculate their sums
- List<int> subset = new List<int>();
- MakeSubset(0, subset);
- // sorted collected subsets of sum N
- for (int i = 0; i < subsets.Count; i++)
- {
- subsets[i].Sort();
- }
- // remove duplicate subsets
- var finalList = subsets.GroupBy(x => String.Join(",", x))
- .Select(x => x.First().ToList())
- .ToList();
- // sort by subset count and value of frst element
- // the subset count determines the number of necessary operands
- var sorted = finalList.OrderBy(list => list.Count).ThenBy(list => list[0]);
- // print the subsets
- foreach (var list in sorted)
- {
- PrintSubset(list);
- }
- // if no sum matches N
- if (!solution)
- Console.WriteLine("No matching subsets.");
- }
- 8 zadacha //////////////////////////////////////////////////////////////////////////////////////////////
- int n = int.Parse(Console.ReadLine());
- string[][] first = new string[n][];
- string[][] second = new string[n][];
- char[] emptySpace = new char[] { ' ', '\t' };
- for (int i = 0; i < n; i++)//fill first array
- {
- string firstRaw = Console.ReadLine().Trim();
- first[i] = firstRaw.Split(emptySpace, StringSplitOptions.RemoveEmptyEntries).ToArray().ToArray();
- }
- for (int i = 0; i < n; i++)// fill second array
- {
- string secondRaw = Console.ReadLine().Trim();
- second[i] = secondRaw.Split(emptySpace,StringSplitOptions.RemoveEmptyEntries).ToArray().ToArray();
- Array.Reverse(second[i]);//reversed array
- }
- bool check = true;// for check if arrays are fit
- //check for null string
- int legoRowLenght = first[0].Length + second[0].Length;
- int counterAllBlocks = legoRowLenght;//counter for blocks in all lego if arrays not are fit
- for (int i = 1; i < n; i++)//check if array fit each other
- {
- int leftSide = first[i].Length;
- int rightSide=second[i].Length;
- counterAllBlocks += leftSide+rightSide;
- if (first[i].Length + second[i].Length != legoRowLenght)
- {
- check = false;
- }
- }
- if (check)
- {
- string[][] legoFit = new string[n][];
- for (int i = 0; i < n; i++)//combine two array in one not jagged array
- {
- legoFit[i] = first[i].Concat(second[i]).ToArray();
- }
- for (int i = 0; i < n; i++)//print lego
- {
- Console.Write("[");
- Console.Write(string.Join(", ", legoFit[i]));
- Console.Write("]");
- Console.WriteLine();
- }
- }
- else
- {
- Console.WriteLine("The total number of cells is: " + counterAllBlocks);
- }
- 9 zadacha ///////////////////////////////////////////////////////////////////////////////////////////////////////
- int n = int.Parse(Console.ReadLine());
- string[] numbers = Console.ReadLine().Split(new char[] { ',', ';', ' ' },
- StringSplitOptions.RemoveEmptyEntries);
- bool stuckNums = false;
- for (int p1 = 0; p1 < n; p1++)
- {
- for (int p2 = 0; p2 < n; p2++)
- {
- for (int p3 = 0; p3 < n; p3++)
- {
- for (int p4 = 0; p4 < n; p4++)
- {
- if (p1 != p2 && p1 != p3 && p1 != p4 && p2 != p3 && p2 != p4 && p3 != p4)
- {
- string left = numbers[p1] + numbers[p2];
- string right = numbers[p3] + numbers[p4];
- if (left == right)
- {
- Console.WriteLine("{0}|{1}=={2}|{3}", numbers[p1], numbers[p2], numbers[p3], numbers[p4]);
- stuckNums = true;
- }
- }
- }
- }
- }
- }
- if (!stuckNums)
- {
- Console.WriteLine("No");
- }
- 10 zadacha ////////////////////////////////////////////////////////////////////////////////////////////////////
- int n = int.Parse(Console.ReadLine());
- int[] nums=new int[n];
- bool pitagoreanEquation = false;
- for (int i = 0; i < n; i++)
- {
- nums[i] = int.Parse(Console.ReadLine());
- }
- for (int p1 = 0; p1 < n; p1++)
- {
- for (int p2 = 0; p2 < n; p2++)
- {
- for (int p3 = 0; p3 < n; p3++)
- {
- if ((nums[p1]*nums[p1] +nums[p2]*nums[p2])==nums[p3]*nums[p3] && nums[p1]<=nums[p2])
- {
- Console.WriteLine("{0}*{0} + {1}*{1} = {2}*{2}", nums[p1], nums[p2], nums[p3]);
- pitagoreanEquation = true;
- }
- }
- }
- }
- if (!pitagoreanEquation)
- {
- Console.WriteLine("No");
- }
Advertisement
Add Comment
Please, Sign In to add comment