n4wn4w

C# [HOMEWORK] - ARRAY, LIST, STACK, OPASHKA

May 5th, 2015
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.86 KB | None | 0 0
  1.  
  2.    List<int> k = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 11, 10, 11, 12, 13 };
  3.             List<int> kuk = new List<int>() { 1, 2, 3, 4, 5, 9, 13, 14, 15, 16, 16, 15 };
  4.  
  5.             List<int> firstList = new List<int>();
  6.  
  7.             firstList.AddRange(k);
  8.             firstList.AddRange(kuk);
  9.  
  10.             firstList.Sort();
  11.             Int32 index = 0;
  12.  
  13.             for (int i = 0; i < k.Count + kuk.Count - 1; i++)
  14.             {
  15.                 if (firstList[index] == firstList[index + 1])
  16.                     firstList.RemoveAt(index);
  17.                 else
  18.                 {
  19.                     index++;
  20.                 }
  21.  
  22.             }
  23.  
  24.             for (int i = 0; i < firstList.Count; i++)
  25.             {
  26.                 Console.WriteLine(firstList[i]);
  27.             }
  28.  
  29.  
  30.  
  31.  
  32. 2 zadacha ////////////////////////////////////////////////////////////////////////////////
  33.  
  34. static int[] numbers;
  35.  
  36.         //LINQ sorting
  37.         static void LINQSort()
  38.         {
  39.             var newArray = from n in numbers
  40.                            orderby n
  41.                            select n;
  42.  
  43.             foreach (var num in newArray)
  44.             {
  45.                 Console.Write(num+" ");
  46.             }
  47.         }
  48.         // simple sorting algorithm
  49.         static int[] SelectionSort()
  50.         {
  51.             for (int a = 0; a < numbers.Length - 1; a++)
  52.             {
  53.                 int minValue = a;
  54.                 for (int b = a + 1; b < numbers.Length; b++)
  55.                 {
  56.                     if (numbers[b] < numbers[minValue])
  57.                     {
  58.                         minValue = b;
  59.                     }
  60.                 }
  61.                 int temp = numbers[a];
  62.                 numbers[a] = numbers[minValue];
  63.                 numbers[minValue] = temp;
  64.             }
  65.                 return numbers;
  66.         }
  67.         static void Main(string[] args)
  68.         {
  69.             string input = Console.ReadLine();
  70.             numbers = input.Split().Select(int.Parse).ToArray();
  71.  
  72.             Console.WriteLine("LINQ");
  73.             LINQSort();
  74.             Console.WriteLine();
  75.             Console.WriteLine("Seltection Sort");
  76.             SelectionSort();
  77.             foreach (var num in numbers)
  78.             {
  79.                 Console.Write(num+" ");
  80.             }
  81.             Console.WriteLine();
  82.  
  83.  
  84. 3 zadacha  ////////////////////////////////////////////////////////////////////////////
  85.  
  86.  
  87.  string input = Console.ReadLine();
  88.             double[] numbers = input.Split().Select(double.Parse).ToArray();
  89.             List<double> roundNum = new List<double>();
  90.             List<double> zeroFractNum = new List<double>();
  91.  
  92.             for (int a = 0; a < numbers.Length; a++)
  93.             {
  94.                 if (numbers[a] % 1 != 0)
  95.                 {
  96.                     zeroFractNum.Add(numbers[a]);
  97.                 }
  98.                 else
  99.                 {
  100.                     roundNum.Add(numbers[a]);
  101.                 }
  102.             }
  103.             Console.WriteLine("Non - Zero Fraction Numbers");
  104.             zeroFractNum.ForEach(a => Console.Write(a + " "));
  105.             Console.WriteLine("min -- {0}", zeroFractNum.Min());
  106.             Console.WriteLine("max -- {0}", zeroFractNum.Max());
  107.             Console.WriteLine("sum -- {0}", zeroFractNum.Sum());
  108.             Console.WriteLine("average -- {0:f2}", zeroFractNum.Average());
  109.             Console.WriteLine("Round Numbers");
  110.             roundNum.ForEach(b => Console.Write(b + " "));
  111.             Console.WriteLine("min -- {0}", roundNum.Min());
  112.             Console.WriteLine("max -- {0}", roundNum.Max());
  113.             Console.WriteLine("sum -- {0}", roundNum.Sum());
  114.             Console.WriteLine("average -- {0:f2}", roundNum.Average());
  115.  
  116.  
  117. 4 zadacha ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  118.  
  119.  
  120.  List<int> k = new List<int>() { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,
  121.                 15,1,2,3,4,5,6,7,8,9,10,1,2,3
  122.                 ,4,5,6,7,8,9,0, };
  123.  
  124.             k.Sort();
  125.  
  126.             for (int i = 1; i < k.Count + 1; i++)
  127.             {
  128.                 if (i == k.Count)
  129.                 {
  130.                     Console.Write(k[k.Count - 1] + " ");
  131.                 }
  132.                 if (i != k.Count)
  133.                 {
  134.                     if (k[i] == k[i - 1])
  135.                     {
  136.                         Console.Write(k[i] + " ");
  137.                     }
  138.                     else
  139.                     {
  140.                         Console.Write(k[i - 1] + " ");
  141.                         Console.WriteLine();
  142.                     }
  143.                 }
  144.             }
  145.             Console.WriteLine();
  146.  
  147.  
  148. ///////////////////////////////////// 2 reshenie ///////////////////////////////////////////////
  149.  
  150. string[] input = Console.ReadLine().Split();
  151.  
  152.             for (int i = 0; i < input.Length - 1; i++)
  153.             {
  154.                 Console.Write(input[i]+" ");
  155.                 if (!input[i].Equals(input[i + 1]))
  156.                 {
  157.                     Console.WriteLine();
  158.                 }
  159.             }
  160.             Console.Write(input[input.Length-1]);
  161.             Console.WriteLine();
  162.  
  163.  
  164. 5 zadacha ////////////////////////////////////////////////////////////////////////////
  165.  
  166.  
  167.  string input = Console.ReadLine();
  168.             int[] numbers = input.Split().Select(int.Parse).ToArray();
  169.  
  170.             int counter = 1;
  171.             int maxLength = 1;
  172.             int end = 0;
  173.  
  174.             Console.Write(numbers[0]+" ");
  175.             for (int i = 1; i < numbers.Length; i++)
  176.             {
  177.                 if (numbers[i] > numbers[i - 1])
  178.                 {
  179.                     counter++;
  180.                     Console.Write(numbers[i]+" ");                  
  181.                 }
  182.                 else
  183.                 {
  184.                     counter = 1;
  185.                     Console.WriteLine();
  186.                     Console.Write(numbers[i]+" ");
  187.                 }
  188.                 if (counter > maxLength)
  189.                 {
  190.                     maxLength = counter;
  191.                     end = i;
  192.                 }
  193.             }
  194.             Console.WriteLine();
  195.             Console.Write("Longest: ");
  196.             for (int j = end - maxLength + 1; j <= end; j++)
  197.             {
  198.                 Console.Write(numbers[j]+" ");
  199.             }
  200.  
  201.  
  202. /////////////////  7 zadacha ima e produljenie na 6  v neq ima sortirane an outputa  i 2 resheniq ///////////////////////////
  203.  
  204.  
  205.   static List<List<int>> subsets = new List<List<int>>();
  206.     static int[] numbers;
  207.     static int N;
  208.     static bool solution = false;
  209.  
  210.     static void Main()
  211.     {
  212.         Console.Write("Please, enter a value for N: ");
  213.         N = int.Parse(Console.ReadLine());
  214.         Console.WriteLine("Please enter a sequence of numbers, separated by a space: ");
  215.         numbers = Console.ReadLine().Split(' ').Select(int.Parse).Distinct().ToArray();
  216.         Array.Sort(numbers);
  217.         //numbers = new int[] { 1, 2, 3, 4 };
  218.         Console.WriteLine("\nOutput:");
  219.         List<int> subsetList = new List<int>();
  220.         MakeSubset(0, subsetList);
  221.         var sorted = subsets.OrderBy(x => x.Count);
  222.         foreach (var item in sorted)
  223.         {
  224.             Console.WriteLine(" {0} = {1}", string.Join(" + ", item), N);
  225.         }
  226.         if (!solution)// if no sum matches N
  227.             Console.WriteLine("No matching subsets.");
  228.     }
  229.  
  230.     static void MakeSubset(int index, List<int> subset)
  231.     {
  232.         if (subset.Sum() == N && subset.Count > 0) // if subset sum = N, print it on the console
  233.         {
  234.             subsets.Add(new List<int>(subset));
  235.             solution = true; // set solution to true, and we will not be printing that there is no solution
  236.         }
  237.         //Console.WriteLine(string.Join(" ", subset));
  238.         for (int i = index; i < numbers.Length; i++)
  239.         {
  240.             subset.Add(numbers[i]);
  241.             MakeSubset(i + 1, subset); // call MakeSubset recursively, every time starting from the previous index + 1
  242.             subset.RemoveAt(subset.Count - 1); // remove last element
  243.         }
  244.     }
  245.  
  246.  
  247. /////////////////////////   2 reshenie /////////////////////////////////////////////////////////////////
  248.  
  249.  
  250.  static List<List<int>> subsets = new List<List<int>>();
  251.     static int[] numbers;
  252.     static int N;
  253.     static bool solution = false;
  254.  
  255.     private static void PrintSubset(List<int> subset)
  256.     {
  257.         if (subset.Count == 1)
  258.         {
  259.             Console.WriteLine(subset[0]);
  260.         }
  261.         else
  262.         {
  263.             // print subset elements 0 to (subset.Count - 1), along with the operators
  264.             for (int i = 0; i < subset.Count; i++)
  265.             {
  266.                 if (i == 0)
  267.                 {
  268.                     Console.Write("{0} +", subset[i]);
  269.                 }
  270.                 else if (i > 0 && i < subset.Count - 1)
  271.                 {
  272.                     Console.Write(" {0} +", subset[i]);
  273.                 }
  274.                 else if (i == subset.Count - 1)
  275.                 {
  276.                     Console.Write(" {0} = {1}", subset[i], N);
  277.                 }
  278.             }
  279.             Console.WriteLine();
  280.         }
  281.     }
  282.  
  283.     private static int CalculateSum(List<int> subset)
  284.     {
  285.         int sum = 0;
  286.         for (int i = 0; i < subset.Count; i++)
  287.             sum += subset[i];
  288.         return sum;
  289.     }
  290.  
  291.     // when calling the method, we set the start index
  292.     // this is why there is no value for index in the method itself
  293.     static void MakeSubset(int index, List<int> subset)
  294.     {
  295.         int sum = CalculateSum(subset);
  296.         if (sum == N) // if subset sum = N, store subset list in the subsets list of lists
  297.         {
  298.             subsets.Add(new List<int>(subset));
  299.  
  300.             solution = true; // set solution to true, and we will not be printing that there is no solution
  301.         }
  302.  
  303.         if (subset.Count == numbers.Length) // if susbset size = input array size
  304.             return; // there is nothing more to be done, return
  305.  
  306.         for (int i = index; i < numbers.Length; i++)
  307.         {
  308.             subset.Add(numbers[i]);
  309.             MakeSubset(i + 1, subset); // call MakeSubset recursively, every time starting from the previous index + 1
  310.             subset.RemoveAt(subset.Count - 1); // remove last element
  311.         }
  312.     }
  313.  
  314.     static void Main()
  315.     {
  316.         // input N
  317.         Console.Write("Please, enter a value for N: ");
  318.         N = int.Parse(Console.ReadLine());
  319.  
  320.         // input array
  321.         Console.WriteLine("Please enter a sequence of numbers, separated by a space: ");
  322.         numbers = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  323.         Console.WriteLine("\nOutput:");
  324.  
  325.         // logic: make subsets, calculate their sums
  326.         List<int> subset = new List<int>();
  327.         MakeSubset(0, subset);
  328.  
  329.         // sorted collected subsets of sum N
  330.         for (int i = 0; i < subsets.Count; i++)
  331.         {
  332.             subsets[i].Sort();
  333.         }
  334.         // remove duplicate subsets
  335.         var finalList = subsets.GroupBy(x => String.Join(",", x))
  336.      .Select(x => x.First().ToList())
  337.      .ToList();
  338.  
  339.         // sort by subset count and value of frst element
  340.         // the subset count determines the number of necessary operands
  341.         var sorted = finalList.OrderBy(list => list.Count).ThenBy(list => list[0]);
  342.  
  343.         // print the subsets
  344.         foreach (var list in sorted)
  345.         {
  346.             PrintSubset(list);
  347.         }
  348.  
  349.         // if no sum matches N
  350.         if (!solution)
  351.             Console.WriteLine("No matching subsets.");
  352.     }
  353.  
  354.  
  355.  
  356. 8 zadacha //////////////////////////////////////////////////////////////////////////////////////////////
  357.  
  358.  
  359.  int n = int.Parse(Console.ReadLine());
  360.         string[][] first = new string[n][];
  361.         string[][] second = new string[n][];
  362.  
  363.         char[] emptySpace = new char[] { ' ', '\t' };
  364.  
  365.         for (int i = 0; i < n; i++)//fill first array
  366.         {
  367.             string firstRaw = Console.ReadLine().Trim();
  368.             first[i] = firstRaw.Split(emptySpace, StringSplitOptions.RemoveEmptyEntries).ToArray().ToArray();        
  369.         }
  370.  
  371.         for (int i = 0; i < n; i++)// fill second array
  372.         {
  373.             string secondRaw = Console.ReadLine().Trim();
  374.             second[i] = secondRaw.Split(emptySpace,StringSplitOptions.RemoveEmptyEntries).ToArray().ToArray();
  375.             Array.Reverse(second[i]);//reversed array
  376.         }
  377.  
  378.         bool check = true;// for check if arrays are fit
  379.  
  380.         //check for null string
  381.  
  382.  
  383.         int legoRowLenght = first[0].Length + second[0].Length;
  384.         int counterAllBlocks = legoRowLenght;//counter for blocks in all lego if arrays not are fit
  385.  
  386.         for (int i = 1; i < n; i++)//check if array fit each other
  387.         {
  388.             int leftSide = first[i].Length;
  389.             int rightSide=second[i].Length;
  390.             counterAllBlocks += leftSide+rightSide;
  391.             if (first[i].Length + second[i].Length != legoRowLenght)
  392.             {
  393.                 check = false;
  394.             }
  395.         }
  396.  
  397.  
  398.         if (check)
  399.         {
  400.             string[][] legoFit = new string[n][];
  401.  
  402.             for (int i = 0; i < n; i++)//combine two array in one not jagged array
  403.             {
  404.                 legoFit[i] = first[i].Concat(second[i]).ToArray();
  405.             }            
  406.  
  407.             for (int i = 0; i < n; i++)//print lego
  408.             {
  409.                 Console.Write("[");
  410.                 Console.Write(string.Join(", ", legoFit[i]));
  411.                 Console.Write("]");
  412.                 Console.WriteLine();
  413.             }
  414.         }
  415.         else
  416.         {
  417.             Console.WriteLine("The total number of cells is: " + counterAllBlocks);
  418.         }
  419.  
  420. 9 zadacha ///////////////////////////////////////////////////////////////////////////////////////////////////////
  421.  
  422. int n = int.Parse(Console.ReadLine());
  423.         string[] numbers = Console.ReadLine().Split(new char[] { ',', ';', ' ' },
  424.             StringSplitOptions.RemoveEmptyEntries);
  425.         bool stuckNums = false;
  426.         for (int p1 = 0; p1 < n; p1++)
  427.         {
  428.             for (int p2 = 0; p2 < n; p2++)
  429.             {
  430.                 for (int p3 = 0; p3 < n; p3++)
  431.                 {
  432.                     for (int p4 = 0; p4 < n; p4++)
  433.                     {
  434.                         if (p1 != p2 && p1 != p3 && p1 != p4 && p2 != p3 && p2 != p4 && p3 != p4)
  435.                         {
  436.                             string left = numbers[p1] + numbers[p2];
  437.                             string right = numbers[p3] + numbers[p4];
  438.                             if (left == right)
  439.                             {
  440.                                 Console.WriteLine("{0}|{1}=={2}|{3}", numbers[p1], numbers[p2], numbers[p3], numbers[p4]);
  441.                                 stuckNums = true;
  442.                             }
  443.                         }
  444.                     }
  445.                 }
  446.             }
  447.         }
  448.         if (!stuckNums)
  449.         {
  450.             Console.WriteLine("No");
  451.         }
  452.  
  453.  
  454. 10 zadacha ////////////////////////////////////////////////////////////////////////////////////////////////////
  455.  
  456.  
  457. int n = int.Parse(Console.ReadLine());
  458.         int[] nums=new int[n];
  459.         bool pitagoreanEquation = false;
  460.         for (int i = 0; i < n; i++)
  461.         {
  462.             nums[i] = int.Parse(Console.ReadLine());
  463.         }
  464.         for (int p1 = 0; p1 < n; p1++)
  465.         {
  466.             for (int p2 = 0; p2 < n; p2++)
  467.             {
  468.                 for (int p3 = 0; p3 < n; p3++)
  469.                 {
  470.                     if ((nums[p1]*nums[p1] +nums[p2]*nums[p2])==nums[p3]*nums[p3] && nums[p1]<=nums[p2])
  471.                     {
  472.                         Console.WriteLine("{0}*{0} + {1}*{1} = {2}*{2}", nums[p1], nums[p2], nums[p3]);
  473.                         pitagoreanEquation = true;
  474.                     }
  475.                 }
  476.             }
  477.         }
  478.         if (!pitagoreanEquation)
  479.         {
  480.             Console.WriteLine("No");
  481.         }
Advertisement
Add Comment
Please, Sign In to add comment