Advertisement
n4wn4w

C# judge system

May 27th, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 41.62 KB | None | 0 0
  1. ///////////////////////////////////// Java Basics Exam 3 September 2014 ///////////////////////////////////////////////////////////
  2.  
  3. 01. Dozen Eggs/////////////////////////////////////////////////////////////////
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9.  
  10. namespace ConsoleApplication1
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             string lineContents = String.Empty;
  17.  
  18.             string[] eventTokens;
  19.             string[] STRnumbers1;
  20.             string kor = "";
  21.             int hoi = 0;
  22.  
  23.             int sumEggs = 0;
  24.             for (int i = 0; i < 7; i++)
  25.             {
  26.                 lineContents = Console.ReadLine();
  27.                 eventTokens = lineContents.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  28.  
  29.                 hoi = int.Parse(eventTokens[0]);
  30.                 kor = eventTokens[1];
  31.  
  32.                
  33.                 if (kor.Equals("eggs"))
  34.                 {
  35.                     sumEggs += hoi;
  36.                 }
  37.                 else if (kor.Equals("dozens"))
  38.                 {
  39.                     sumEggs += hoi * 12;
  40.                 }
  41.             }
  42.            
  43.             int kondio = sumEggs / 12;
  44.             int kila = sumEggs % 12;
  45.  
  46.             Console.WriteLine("{0} dozens + {1} eggs", kondio, kila);
  47.            
  48.         }
  49.     }
  50. }
  51.  
  52.  03. Biggest 3 Prime Numbers   /////////////////////////////////////////////////////////////////////
  53.  
  54. using System;
  55. using System.Collections.Generic;
  56. using System.Linq;
  57. using System.Text;
  58. using System.Threading.Tasks;
  59.  
  60. class Program
  61. {
  62.     static void Main(string[] args)
  63.     {
  64.         string input = Console.ReadLine();
  65.         string[] inputTokens = input.Split(new char[] { ' ', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
  66.         int[] numbers = inputTokens.Select(int.Parse).ToArray(); // converting string to int[];
  67.  
  68.         List<int> primes = new List<int>();             // Collecting prime numbers
  69.  
  70.         for (int i = 0; i < numbers.Length; i++)
  71.         {
  72.             if (isPrime(numbers[i]))
  73.             {
  74.                 primes.Add(numbers[i]);
  75.             }
  76.         }
  77.    
  78.         primes.Sort();
  79.         int index = 0;
  80.         while (index < primes.Count - 1) // remove repeating numbers
  81.         {
  82.             if (primes[index] == primes[index + 1])
  83.                 primes.RemoveAt(index);
  84.             else
  85.                 index++;
  86.         }
  87.         primes.Reverse();
  88.  
  89.         //foreach (var item in primes)
  90.         //{
  91.         //    Console.WriteLine(item);
  92.         //}
  93.  
  94.         for (int i = 0; i < primes.Count; i++)
  95.         {
  96.             if (primes.Count < 3)
  97.             {
  98.                 Console.WriteLine("No");
  99.                 break;
  100.             }
  101.             else
  102.             {
  103.                 Console.WriteLine(primes[0] + primes[1] + primes[2]);
  104.                 break;
  105.             }
  106.                    
  107.         }
  108.     }
  109.  
  110.  
  111.     public static bool isPrime(int number)
  112.     {    
  113.             if ((number & 1) == 0)
  114.             {
  115.                 if (number == 2)
  116.                 {
  117.                     return true;
  118.                 }
  119.                 else
  120.                 {
  121.                     return false;
  122.                 }
  123.             }
  124.             // Note:
  125.             // ... This version was changed to test the square.
  126.             // ... Original version tested against the square root.
  127.             // ... Also we exclude 1 at the end.
  128.         for (int i = 3; (i * i) <= number; i += 2)
  129.         {
  130.             if ((number % i) == 0)
  131.             {
  132.                 return false;
  133.             }
  134.         }
  135.         return number != 1;
  136.         }
  137.     }
  138.  
  139.  
  140. ////////////////////////////////////////////// 04. Activity Tracker   ///////////////////////////////////////////////////////////
  141.  
  142. using System;
  143. using System.Collections.Generic;
  144.  
  145. class ActivityTracker
  146. {
  147.     static void Main()
  148.     {
  149.         SortedDictionary<int, SortedDictionary<string, int>> dataDictionary =
  150.                                             new SortedDictionary<int, SortedDictionary<string, int>>();
  151.  
  152.         int dataLinesNumber = int.Parse(Console.ReadLine());
  153.         string lineContents = String.Empty;
  154.         string[] lineTokens;
  155.         int month = 0;
  156.         string user = String.Empty;
  157.         int distance = 0;
  158.         for (int i = 0; i < dataLinesNumber; i++)
  159.         {
  160.             lineContents = Console.ReadLine();
  161.             lineTokens = lineContents.Split(new char[] { ' ', '/' }, StringSplitOptions.RemoveEmptyEntries);
  162.             month = int.Parse(lineTokens[1]);
  163.             user = lineTokens[3];
  164.             distance = int.Parse(lineTokens[4]);
  165.  
  166.             if (!dataDictionary.ContainsKey(month))
  167.             {
  168.                 dataDictionary[month] = new SortedDictionary<string, int>();
  169.             }
  170.  
  171.             if (!dataDictionary[month].ContainsKey(user))
  172.             {
  173.                 dataDictionary[month][user] = distance;
  174.             }
  175.             else
  176.             {
  177.                 dataDictionary[month][user] += distance;
  178.             }
  179.         }
  180.  
  181.         bool isFirstPair = true;
  182.         foreach (var dataPair in dataDictionary)
  183.         {
  184.             Console.Write("{0}: ", dataPair.Key);
  185.             isFirstPair = true;
  186.             foreach (var subPair in dataPair.Value)
  187.             {
  188.                 if (isFirstPair)
  189.                 {
  190.                     Console.Write("{0}({1})", subPair.Key, subPair.Value);
  191.                     isFirstPair = false;
  192.                 }
  193.                 else
  194.                 {
  195.                     Console.Write(", {0}({1})", subPair.Key, subPair.Value);
  196.                 }
  197.             }
  198.             Console.WriteLine();
  199.         }
  200.     }
  201. }
  202.  
  203.  
  204. ///////////////////////////////////////////////////////// Java Basics Exam 26 May 2014   //////////////////////////////////////////
  205.  
  206.  
  207. 04 zadacha Couples Frequency  /////////////////////////////////////////////////////////////////////////////
  208.  
  209. using System;
  210. using System.Collections.Generic;
  211. using System.Linq;
  212.  
  213. public class CouplesFrequency
  214. {
  215.     public static void Main()
  216.     {
  217.         int[] numbers = Console.ReadLine()
  218.             .Split()
  219.             .Select(int.Parse)
  220.             .ToArray();
  221.  
  222.         int totalNumberOfCouples = numbers.Length - 1;
  223.  
  224.         var coupleFrequencies = new Dictionary<string, int>();
  225.  
  226.         for (int i = 1; i < numbers.Length; i++)
  227.         {
  228.             string currentCouple = string.Format(
  229.                 "{0} {1}",
  230.                 numbers[i - 1],
  231.                 numbers[i]);
  232.  
  233.             if (!coupleFrequencies.ContainsKey(currentCouple))
  234.             {
  235.                 coupleFrequencies.Add(currentCouple, 0);
  236.             }
  237.  
  238.             coupleFrequencies[currentCouple]++;
  239.         }
  240.  
  241.         foreach (var coupleFrequency in coupleFrequencies)
  242.         {
  243.             double frequency = coupleFrequency.Value * 100.0 / totalNumberOfCouples;
  244.  
  245.             Console.WriteLine(
  246.                 "{0} -> {1:F2}%",
  247.                 coupleFrequency.Key,
  248.                 frequency);
  249.         }
  250.     }
  251. }
  252.  
  253.  
  254. 03 zadacha Largest 3 Rectangles   ////////////////////////////////////////////////////////////////////////////////////////////
  255.  
  256.  
  257.  
  258. using System;
  259. using System.Collections.Generic;
  260. using System.Linq;
  261. using System.Text;
  262.  
  263. namespace ConsoleApplication1
  264. {
  265.     class Program
  266.     {
  267.         static void Main(string[] args)
  268.         {
  269.  
  270.  
  271.             string[] STRnumbers = Console.ReadLine().Split(new char[] { ' ','X','[',']','x' }, StringSplitOptions.RemoveEmptyEntries);
  272.  
  273.            
  274.             int[] nums = Array.ConvertAll<string, int>(STRnumbers, int.Parse);
  275.  
  276.             List<int> listOfStrings = new List<int>();
  277.  
  278.  
  279.             for (int i = 0; i < nums.Length; i += 2)
  280.             {
  281.                 int hoi = nums[i] * nums[i + 1];
  282.                 listOfStrings.Add(hoi);
  283.             }
  284.  
  285.             int sum = 0;
  286.             int bestSum = 0;
  287.             for (int i = 0; i < listOfStrings.Count-2; i++)
  288.             {
  289.                 sum = 0;
  290.                 for (int j = i; j < 3 + i; j++)
  291.                 {
  292.                     sum += listOfStrings[j];
  293.  
  294.                 }
  295.                 if (sum > bestSum)
  296.                 {
  297.                     bestSum = sum;
  298.                 }
  299.             }
  300.             Console.WriteLine(bestSum);
  301.         }
  302.     }
  303. }
  304.  
  305.  
  306. ////////////////////////////////////////// Java Basics Exam 27 May 2014   ////////////////////////////////////////////////////
  307.  
  308.  
  309. 04 zadacha   ORDERS  ///////////////////////////////////////////////////////////////////////////////////////////
  310. using System;
  311. using System.Collections.Generic;
  312. using System.Linq;
  313. using System.Text;
  314.  
  315. namespace ConsoleApplication2
  316. {
  317.     class Program
  318.     {
  319.         static void Main(string[] args)
  320.         {
  321.  
  322.  
  323.             Dictionary<string, SortedDictionary<string, int>> dataDictionary =
  324.                                            new Dictionary<string, SortedDictionary<string, int>>();
  325.  
  326.             int dataLinesNumber = int.Parse(Console.ReadLine());
  327.             string lineContents = String.Empty;
  328.             string[] lineTokens;
  329.             string fruit = "";
  330.             string user = String.Empty;
  331.             int kolichestvo = 0;
  332.             for (int i = 0; i < dataLinesNumber; i++)
  333.             {
  334.                 lineContents = Console.ReadLine();
  335.                 lineTokens = lineContents.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  336.                 fruit = (lineTokens[2]);
  337.                 user = lineTokens[0];
  338.                 kolichestvo = int.Parse(lineTokens[1]);
  339.  
  340.                 if (!dataDictionary.ContainsKey(fruit))
  341.                 {
  342.                     dataDictionary[fruit] = new SortedDictionary<string, int>();
  343.                 }
  344.  
  345.                 if (!dataDictionary[fruit].ContainsKey(user))
  346.                 {
  347.                     dataDictionary[fruit][user] = kolichestvo;
  348.                 }
  349.                 else
  350.                 {
  351.                     dataDictionary[fruit][user] += kolichestvo;
  352.                 }
  353.             }
  354.  
  355.             bool isFirstPair = true;
  356.             foreach (var dataPair in dataDictionary)
  357.             {
  358.                 Console.Write("{0}: ", dataPair.Key);
  359.                 isFirstPair = true;
  360.                 foreach (var subPair in dataPair.Value)
  361.                 {
  362.                     if (isFirstPair)
  363.                     {
  364.                         Console.Write("{0} {1}", subPair.Key, subPair.Value);
  365.                         isFirstPair = false;
  366.                     }
  367.                     else
  368.                     {
  369.                         Console.Write(", {0} {1}", subPair.Key, subPair.Value);
  370.                     }
  371.                 }
  372.                 Console.WriteLine();
  373.  
  374.  
  375.             }
  376.         }
  377.     }
  378. }
  379.  
  380.  03 zadacha Longest Odd-Even Sequence  ////////////////////////////////////////////////////////////////////////////////  
  381.  
  382. using System;
  383. using System.Collections.Generic;
  384. using System.Text.RegularExpressions;
  385. using System.Text;
  386.  
  387.  class UseYourChainsBuddy
  388.     {
  389.         static void Main(string[] args)
  390.         {
  391.  
  392.  
  393.  
  394.  
  395.  
  396.             string[] STRnumbers = Console.ReadLine().Split(new char[] { ' ','(',')' }, StringSplitOptions.RemoveEmptyEntries);
  397.  
  398.            
  399.             int[] nums = Array.ConvertAll<string, int>(STRnumbers, int.Parse);
  400.  
  401.             int count = 0;
  402.  
  403.            int bestLen = 0;
  404.         int len = 0;
  405.         bool shouldBeOdd = (nums[0] % 2 != 0);
  406.             for (int i = 0; i < nums.Length; i++)
  407.             {
  408.              bool isOdd = nums[i] % 2 != 0;
  409.             if (isOdd == shouldBeOdd || nums[i] == 0) {
  410.                 len++;
  411.             } else {
  412.                 shouldBeOdd = isOdd;
  413.                 len = 1;
  414.             }
  415.             shouldBeOdd = !shouldBeOdd;
  416.             if (len > bestLen) {
  417.                 bestLen = len;
  418.             }
  419.             }
  420.             Console.WriteLine(bestLen);
  421.         }
  422.     }
  423.  
  424.  
  425. //////////////////////////Java Basics Exam 1 June 2014  //////////////////////////////////////////////////////////////////////
  426.  
  427.  
  428. 03 zadacha Simple Expression  /////////////////////////////////////////////////////////////////////////////////////
  429.  
  430. using System;
  431. using System.Collections.Generic;
  432. using System.Linq;
  433. using System.Text;
  434. using System.Collections;
  435. using System.Text.RegularExpressions;
  436.  
  437. namespace ConsoleApplication1
  438. {
  439.     class Program
  440.     {
  441.         static void Main(string[] args)
  442.         {
  443.             string text = Console.ReadLine();
  444.  
  445.             string regex = @"\+|\-";
  446.  
  447.           //  Stack words = new Stack();
  448.             Queue separators = new Queue();
  449.             Queue digits = new Queue();
  450.  
  451.  
  452.             string[] wordsInSentence = Regex.Split(text, regex);
  453.  
  454.  
  455.             foreach (var w in wordsInSentence)
  456.             {  
  457.                    digits.Enqueue(w.Trim());
  458.             }
  459.  
  460.             MatchCollection separatorsInSentance = Regex.Matches(text, regex);
  461.  
  462.             foreach (var s in separatorsInSentance)
  463.             {
  464.                 separators.Enqueue(s);
  465.             }
  466.  
  467.             StringBuilder reversSentance = new StringBuilder();
  468.  
  469.             while (digits.Count > 0 || separators.Count > 0)
  470.             {
  471.                 reversSentance.Append(digits.Dequeue());
  472.                 if (digits.Count == 0)
  473.                 {
  474.                     continue;
  475.                 }
  476.                 reversSentance.Append(" ");
  477.                
  478.                 reversSentance.Append(separators.Dequeue());
  479.                
  480.             }
  481.  
  482.           string builder =   reversSentance.ToString();
  483.  
  484.           string[] STRnumbers = builder.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  485.  
  486.          // int[] array = new int[STRnumbers.Length];
  487.        //   decimal[] array = Array.ConvertAll<string, decimal>(STRnumbers, decimal.Parse);
  488.  
  489.         //  decimal sum = array.Sum();
  490.  
  491.         //  Console.WriteLine("{0:F9}",sum);
  492.  
  493.  
  494.            List<string> hoi = new List<string>();
  495.            // listofIDs.Select(int.Parse).ToList()
  496.            hoi.Add(STRnumbers[0]);
  497.            string currvalue = STRnumbers[0];
  498.  
  499.            for (int i = 1; i < STRnumbers.Length; i++)
  500.            {
  501.                hoi.Add(STRnumbers[i]);
  502.            }
  503.  
  504.            decimal total = hoi.Sum(x => Convert.ToDecimal(x));
  505.            //    hoi.select(int.parse).tolist();
  506.            //   ;
  507.  
  508.            Console.WriteLine("{0:F9}",total);
  509.         }
  510.     }
  511. }
  512.  
  513.  
  514. 04 zadacha  Logs Aggregator  ///////////////////////////////////////////////////////////////////////////////////////////
  515.  
  516. using System;
  517. using System.Collections.Generic;
  518. using System.Linq;
  519.  
  520. class LogsAggregator
  521. {
  522.     static void Main()
  523.     {
  524.         int n = int.Parse(Console.ReadLine());
  525.  
  526.         SortedDictionary<string, SortedDictionary<string, int>> dataDic =
  527.                             new SortedDictionary<string, SortedDictionary<string, int>>();
  528.  
  529.         string input = string.Empty;
  530.         string[] inputTokens;
  531.         string name = string.Empty;
  532.         string ip = string.Empty;
  533.         int number;
  534.  
  535.         for (int i = 0; i < n; i++)
  536.         {
  537.             input = Console.ReadLine();
  538.             inputTokens = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  539.             name = inputTokens[1];
  540.             ip = inputTokens[0];
  541.             number = int.Parse(inputTokens[2]);
  542.  
  543.             if (!dataDic.ContainsKey(name))
  544.             {
  545.                 dataDic[name] = new SortedDictionary<string, int>();
  546.             }
  547.             if (!dataDic[name].ContainsKey(ip))
  548.             {
  549.                 dataDic[name][ip] = number;
  550.             }
  551.             else
  552.             {
  553.                 dataDic[name][ip] += number;
  554.             }
  555.  
  556.         }
  557.  
  558.  
  559.         //output
  560.  
  561.  
  562.         foreach (var entry in dataDic)
  563.         {
  564.             string ips = String.Empty;
  565.             int finalDuration = 0;
  566.  
  567.             foreach (var innnerEntry in entry.Value)
  568.             {
  569.                 ips += innnerEntry.Key;
  570.                 ips += " ";
  571.                 finalDuration += innnerEntry.Value;
  572.             }
  573.  
  574.             Console.WriteLine(entry.Key + ": " + finalDuration + " [" + ips.Trim().Replace(" ", ", ") + "]");
  575.         }
  576.     }
  577. }
  578.  
  579. ///////////////////////////////////   Java Basics - 22 uni ///////////////////////////////////////////////////////////////////////
  580.  
  581.  
  582. using System;
  583. using System.Linq;
  584. using System.Text;
  585. using System.Text.RegularExpressions;
  586. using System.Collections.Generic;
  587.  
  588. public class LittleJohn
  589. {
  590.     public static void Main()
  591.     {
  592.  
  593.  
  594.         string STRnumbers = Console.ReadLine();
  595.  
  596.         string regex = @"[a-zA-Z]*";
  597.  
  598.         MatchCollection separatorsInSentance = Regex.Matches(STRnumbers, regex);
  599.  
  600.         List<string> words = new List<string>();
  601.  
  602.         foreach (var w in separatorsInSentance)
  603.         {
  604.             if (w.ToString() != "")
  605.             {
  606.                 words.Add(w.ToString());
  607.             }
  608.         }
  609.  
  610.         HashSet<string> cognateWords = new HashSet<string>();
  611.  
  612.         for (int a = 0; a < words.Count; a++)
  613.         {
  614.             for (int b = 0; b < words.Count; b++)
  615.             {
  616.                 for (int c = 0; c < words.Count; c++)
  617.                 {
  618.                     // Check if a!=b and a|b=c
  619.                     if (a != b)
  620.                     {
  621.                         bool check = (words[a].ToString() + words[b].ToString()).Equals(words[c].ToString());
  622.                         if (check)
  623.                         {
  624.                             cognateWords.Add(words[a].ToString() + "|" + words[b].ToString() + "=" + words[c].ToString());
  625.                         }
  626.                     }
  627.                 }
  628.             }
  629.         }
  630.         if (cognateWords.Count == 0)
  631.         {
  632.             Console.WriteLine("No");
  633.         }
  634.         else
  635.         {
  636.             int i = 0;
  637.             foreach (var item in cognateWords)
  638.             {
  639.                 Console.WriteLine(item);
  640.             }
  641.         }
  642.     }
  643. }
  644.  
  645.  
  646.  
  647.  
  648.  
  649. ////////////////////////////// Java Basics - 21 Sept 2014 Evening  ////////////////////////////////////////////////////////////////////
  650.  
  651.  
  652. 03. Valid Usernames ///////////////////////////////////////////////////////////////////////////////////////////
  653.  
  654.  
  655. using System;
  656. using System.Text.RegularExpressions;
  657.  
  658. class ValidUsernames
  659. {
  660.     static void Main(string[] args)
  661.     {
  662.         string text = Console.ReadLine();
  663.         string pattern = @"\b[a-zA-Z]\w{2,24}\b";
  664.         Regex users = new Regex(pattern);
  665.         MatchCollection matches = users.Matches(text);
  666.         int first = 0;
  667.         int second = 1;
  668.         int bestSum = int.MinValue;
  669.         int sum = 0;
  670.         for (int i = 0; i < matches.Count - 1; i++)
  671.         {
  672.             sum = matches[i].Length + matches[i + 1].Length;
  673.             if (sum > bestSum)
  674.             {
  675.                 bestSum = sum;
  676.                 first = i;
  677.                 second = i + 1;
  678.             }
  679.         }
  680.         Console.WriteLine(matches[first]);
  681.         Console.WriteLine(matches[second]);
  682.     }
  683. }
  684.  
  685.  
  686. 04. Office Stuff  //////////////////////////////////////////////////////////////////////////////////////////////////
  687.  
  688. using System;
  689. using System.Collections.Generic;
  690. using System.Linq;
  691. using System.Text;
  692. using System.Threading.Tasks;
  693.  
  694. namespace ConsoleApplication28
  695. {
  696.     internal class Program // Nasko Lapchev
  697.     {
  698.         private static void Main(string[] args)
  699.         {
  700.             // databases
  701.             SortedDictionary<string,
  702.              Dictionary<string, List<double>>> dataDictionary =
  703.                  new SortedDictionary<string, Dictionary<string, List<double>>>(); // тук събираме студенти, предмети, оценки
  704.  
  705.             // тук слагаме накрая форматираните за печат резултати от събраната информация
  706.             Dictionary<string, HashSet<string>> finals = new Dictionary<string, HashSet<string>>();
  707.  
  708.             //SortedDictionary<string, SortedDictionary<string, double>> dataO =
  709.             //new SortedDictionary<string, SortedDictionary<string, double>>();
  710.  
  711.             // input
  712.             int dataLinesNumber = int.Parse(Console.ReadLine());
  713.  
  714.             // declarations - страхотна идея - дава яснота на кода по-нататък
  715.             string lineContents = String.Empty;
  716.             string[] lineTokens;
  717.             string student = "";
  718.             string predmet = String.Empty;
  719.             int ocenka = 0;
  720.  
  721.             // reading and parsing the lines
  722.             // имаме 3 основни случая:
  723.             // 1. такъв студент още не е добавян:
  724.             // * правим List<double> за оценките му, и прибавяме оценката към този лист
  725.             // * правим SortedDictionary<string, List<double>> studentData, и към него прибавяме предмета, и списъка с оценки (списък за да можем да прибавим и следващи оценки ако се появят
  726.             // * чак сега вече можем да прибавим към  dataDictionary.Add(student, studentData);
  727.             // 2. Вече има такъв студент додразбира се че и SortedDictionary<string, List<double>> studentData вече има, тъй като този речник се прибавя заедно със студента),
  728.             // но предмета е нов: правим List<double> ocenki = new List<double>();, и прибавяме dataDictionary[student].Add(predmet, ocenki);
  729.             // 3. има такъв студент, има такъв предмет, само оценката е нова
  730.             // тогава: dataDictionary[student][predmet].Add(ocenka);
  731.  
  732.             for (int i = 0; i < dataLinesNumber; i++)
  733.             {
  734.                 lineContents = Console.ReadLine();
  735.                 lineTokens = lineContents.Split(new char[] { ' ', '|', '-' }, StringSplitOptions.RemoveEmptyEntries);
  736.                 student = lineTokens[0];
  737.  
  738.                 predmet = lineTokens[2];
  739.                 ocenka = int.Parse(lineTokens[1]);
  740.  
  741.                 if (!dataDictionary.ContainsKey(student))
  742.                 {
  743.                     // ако в речника още няма такъв студент - няма как да прибавяме към него, трябва първо да го създадем
  744.                     // същото с информацията за студента (SortedDictionary<string, List<double>> studentData) -
  745.                     // използвах твоята идея за речника предмет-оценка, но го развих в предмет-оценки: за да може в един List<double> с оценки да пълним всички оценки
  746.                     // между другото - за разлика от теб щях да се сетя че трябва оценките да са double - не по рано от 1 час селд като съм почнала да пиша кода :)
  747.                     Dictionary<string, List<double>> studentData = new Dictionary<string, List<double>>();
  748.                     List<double> ocenki = new List<double>(); // ако искаш може да смениш "ocenka/i" с grade/s
  749.  
  750.                     ocenki.Add(ocenka);
  751.                     studentData.Add(predmet, ocenki);
  752.                     dataDictionary.Add(student, studentData); // чак сега вече можем да добавим нов студент в речника
  753.                 }
  754.  
  755.                 else if (dataDictionary.ContainsKey(student))
  756.                 {
  757.                     if (!dataDictionary[student].ContainsKey(predmet))
  758.                     {
  759.                         //dataDictionary[student][predmet] = new List<double>(); // мисля че няма да можем да прибавим оценки ако няма такъв студент и ние още не сме го добавили
  760.                         // иначе казано, за да го има студента вече в речника, тяй е бил добавен заедно с SortedDictionary<string, List<double>> studentData, и там вече си има друг предмет, с други оценки
  761.                         // остава само да добавим във вътрешня речник studentDatа нов предмет, с оценките към него
  762.                         List<double> ocenki = new List<double>();
  763.                         ocenki.Add(ocenka);
  764.                         dataDictionary[student].Add(predmet, ocenki);
  765.                     }
  766.                     else if (dataDictionary[student].ContainsKey(predmet))
  767.                     {
  768.                         // за да го има предмета, той е бил добавен с List<double> с оценки, сега остава само да прибавим още една оценка
  769.                         dataDictionary[student][predmet].Add(ocenka);
  770.                     }
  771.                 }
  772.             }
  773.  
  774.  
  775.             // сега идеяата е точно както ти попита: да се подготвим за печатане, ще превърнем всичката информация за студента в един форматиран стринг
  776.             foreach (var pair in dataDictionary)
  777.             {
  778.                 HashSet<string> infoPoPredmeti = new HashSet<string>();
  779.                 foreach (var pair2 in pair.Value) // трябва ни само информацияята за студента, без самия студент
  780.                 {
  781.                     string predmet_srednaocenka = String.Format("{1:F2}-{0}", pair2.Value.Sum(), pair2.Key); // така форматираме с колко знака след десетичната точка ще се печата средната оценка
  782.                     //  и за всеки предмет ще получим нещо такова: "history – 5.00"
  783.  
  784.                     infoPoPredmeti.Add(predmet_srednaocenka); // тук са събрани всички стрингове от типа "history – 5.00", за един студент само
  785.                 }
  786.                 finals.Add(pair.Key, infoPoPredmeti); // тук прибавяме студента, със съответната infoPoPredmeti
  787.             }
  788.  
  789.             foreach (var pair in finals)
  790.             {
  791.                 string infoZaStudenta = string.Join(", ", pair.Value);
  792.                 Console.WriteLine("{0}: {1}", pair.Key, infoZaStudenta);
  793.             }
  794.         }
  795.     }
  796. }
  797.  
  798.  
  799.  
  800. ////////////////////////////////  Java Basics - 7 January 2015  ///////////////////////////////////////////////////////////////////
  801.  
  802.  
  803. 01 zadacha PYRAMID
  804.  
  805. using System;
  806. using System.Collections.Generic;
  807. using System.Linq;
  808. using System.Text;
  809.  
  810. namespace ConsoleApplication1
  811. {
  812.     class Program
  813.     {
  814.         static void Main(string[] args)
  815.         {
  816.             int n = int.Parse(Console.ReadLine());
  817.  
  818.             List<int> sequence = new List<int>();
  819.             int previousNumber = int.Parse(Console.ReadLine().Trim());
  820.             sequence.Add(previousNumber);
  821.  
  822.             for (int i = 1; i < n; i++)
  823.             {
  824.                 string line = Console.ReadLine();
  825.                 string[] numbersAsString = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  826.  
  827.                 int[] numbers = new int[numbersAsString.Length];
  828.  
  829.                 for (int j = 0; j < numbersAsString.Length; j++)
  830.                 {
  831.                     numbers[j] = int.Parse(numbersAsString[j]);
  832.                 }
  833.  
  834.                 int minNumber = int.MaxValue;
  835.                 bool foundNumber = false;
  836.                 for (int k = 0; k < numbers.Length; k++)
  837.                 {
  838.                     int currentNumber = numbers[k];
  839.  
  840.                     if (currentNumber < minNumber && currentNumber > previousNumber)
  841.                     {
  842.                         minNumber = currentNumber;
  843.                         foundNumber = true;
  844.                     }
  845.                 }
  846.  
  847.                 if (foundNumber)
  848.                 {
  849.                     sequence.Add(minNumber);
  850.                     previousNumber = minNumber;
  851.                 }
  852.                 else
  853.                 {
  854.                     previousNumber++;
  855.                 }
  856.  
  857.  
  858.             }
  859.  
  860.             Console.WriteLine(string.Join(", ",sequence));
  861.         }
  862.     }
  863. }
  864.  
  865.  
  866.  
  867.  
  868. 02. Terrorists Win!/////////////////////////////////////////////////////////////////////////////////////////////////////////
  869.  
  870. using System;
  871. using System.Text;
  872.  
  873. class TerroristsWin
  874. {
  875.     static void Main()
  876.     {
  877.         string inputText = Console.ReadLine();
  878.  
  879.         StringBuilder inputTextAsStringBuilder = new StringBuilder(inputText);
  880.         int startIndex = inputText.IndexOf('|');
  881.         int endIndex = 0;
  882.         int unicodeSum = 0;
  883.         int destroyedArea = 0;
  884.         while (startIndex != -1)
  885.         {
  886.             unicodeSum = 0;
  887.             endIndex = inputText.IndexOf('|', startIndex + 1);
  888.             if (endIndex <= startIndex)
  889.             {
  890.                 break;
  891.             }
  892.             for (int i = startIndex + 1; i <= endIndex - 1; i++)
  893.             {
  894.                 unicodeSum += inputText[i];
  895.                 inputTextAsStringBuilder[i] = '.';
  896.             }
  897.             destroyedArea = unicodeSum % 10;
  898.  
  899.             for (int i = startIndex; i >= startIndex - destroyedArea && i >= 0; i--)
  900.             {
  901.                 inputTextAsStringBuilder[i] = '.';
  902.             }
  903.             for (int i = endIndex; i <= endIndex + destroyedArea && i < inputTextAsStringBuilder.Length; i++)
  904.             {
  905.                 inputTextAsStringBuilder[i] = '.';
  906.             }
  907.  
  908.             startIndex = inputText.IndexOf('|', endIndex + 1);
  909.         }
  910.         Console.WriteLine(inputTextAsStringBuilder);
  911.     }
  912. }
  913.  
  914. 04. School System ////////////////////////////////////////////////////////////////////////////////////////
  915.  
  916. using System;
  917. using System.Collections.Generic;
  918. using System.Linq;
  919. using System.Text;
  920. using System.Threading.Tasks;
  921.  
  922. namespace ConsoleApplication28
  923. {
  924.     internal class Program // Nasko Lapchev
  925.     {
  926.         private static void Main(string[] args)
  927.         {
  928.             // databases
  929.             SortedDictionary<string,
  930.             SortedDictionary<string, List<double>>> dataDictionary =
  931.                 new SortedDictionary<string, SortedDictionary<string, List<double>>>(); // тук събираме студенти, предмети, оценки
  932.  
  933.             // тук слагаме накрая форматираните за печат резултати от събраната информация
  934.             SortedDictionary<string, SortedSet<string>> finals = new SortedDictionary<string, SortedSet<string>>();
  935.  
  936.             //SortedDictionary<string, SortedDictionary<string, double>> dataO =
  937.                 //new SortedDictionary<string, SortedDictionary<string, double>>();
  938.            
  939.             // input
  940.             int dataLinesNumber = int.Parse(Console.ReadLine());
  941.  
  942.             // declarations - страхотна идея - дава яснота на кода по-нататък
  943.             string lineContents = String.Empty;
  944.             string[] lineTokens;
  945.             string student = "";
  946.             string predmet = String.Empty;
  947.             int ocenka = 0;
  948.  
  949.             // reading and parsing the lines
  950.             // имаме 3 основни случая:
  951.             // 1. такъв студент още не е добавян:
  952.             // * правим List<double> за оценките му, и прибавяме оценката към този лист
  953.             // * правим SortedDictionary<string, List<double>> studentData, и към него прибавяме предмета, и списъка с оценки (списък за да можем да прибавим и следващи оценки ако се появят
  954.             // * чак сега вече можем да прибавим към  dataDictionary.Add(student, studentData);
  955.             // 2. Вече има такъв студент додразбира се че и SortedDictionary<string, List<double>> studentData вече има, тъй като този речник се прибавя заедно със студента),
  956.             // но предмета е нов: правим List<double> ocenki = new List<double>();, и прибавяме dataDictionary[student].Add(predmet, ocenki);
  957.             // 3. има такъв студент, има такъв предмет, само оценката е нова
  958.             // тогава: dataDictionary[student][predmet].Add(ocenka);
  959.  
  960.             for (int i = 0; i < dataLinesNumber; i++)
  961.             {
  962.                 lineContents = Console.ReadLine();
  963.                 lineTokens = lineContents.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  964.                 student = (lineTokens[0]) + " " + (lineTokens[1]);
  965.  
  966.                 predmet = lineTokens[2];
  967.                 ocenka = int.Parse(lineTokens[3]);
  968.  
  969.                 if (!dataDictionary.ContainsKey(student))
  970.                 {
  971.                     // ако в речника още няма такъв студент - няма как да прибавяме към него, трябва първо да го създадем
  972.                     // същото с информацията за студента (SortedDictionary<string, List<double>> studentData) -
  973.                     // използвах твоята идея за речника предмет-оценка, но го развих в предмет-оценки: за да може в един List<double> с оценки да пълним всички оценки
  974.                     // между другото - за разлика от теб щях да се сетя че трябва оценките да са double - не по рано от 1 час селд като съм почнала да пиша кода :)
  975.                     SortedDictionary<string, List<double>> studentData = new SortedDictionary<string, List<double>>();
  976.                     List<double> ocenki = new List<double>(); // ако искаш може да смениш "ocenka/i" с grade/s
  977.  
  978.                     ocenki.Add(ocenka);
  979.                     studentData.Add(predmet, ocenki);
  980.                     dataDictionary.Add(student, studentData); // чак сега вече можем да добавим нов студент в речника
  981.                 }
  982.  
  983.                 else if (dataDictionary.ContainsKey(student))
  984.                 {
  985.                     if (!dataDictionary[student].ContainsKey(predmet))
  986.                     {
  987.                         //dataDictionary[student][predmet] = new List<double>(); // мисля че няма да можем да прибавим оценки ако няма такъв студент и ние още не сме го добавили
  988.                         // иначе казано, за да го има студента вече в речника, тяй е бил добавен заедно с SortedDictionary<string, List<double>> studentData, и там вече си има друг предмет, с други оценки
  989.                         // остава само да добавим във вътрешня речник studentDatа нов предмет, с оценките към него
  990.                         List<double> ocenki = new List<double>();
  991.                         ocenki.Add(ocenka);
  992.                         dataDictionary[student].Add(predmet, ocenki);
  993.                     }
  994.                     else if (dataDictionary[student].ContainsKey(predmet))
  995.                     {
  996.                         // за да го има предмета, той е бил добавен с List<double> с оценки, сега остава само да прибавим още една оценка
  997.                         dataDictionary[student][predmet].Add(ocenka);
  998.                     }
  999.                 }
  1000.             }
  1001.  
  1002.             // сега идеяата е точно както ти попита: да се подготвим за печатане, ще превърнем всичката информация за студента в един форматиран стринг
  1003.             foreach (var pair in dataDictionary)
  1004.             {
  1005.                 SortedSet<string> infoPoPredmeti = new SortedSet<string>();
  1006.                 foreach (var pair2 in pair.Value) // трябва ни само информацияята за студента, без самия студент
  1007.                 {
  1008.                     string predmet_srednaocenka = String.Format("{0} - {1:F2}", pair2.Key, pair2.Value.Average()); // така форматираме с колко знака след десетичната точка ще се печата средната оценка
  1009.                  //  и за всеки предмет ще получим нещо такова: "history – 5.00"
  1010.  
  1011.                     infoPoPredmeti.Add(predmet_srednaocenka); // тук са събрани всички стрингове от типа "history – 5.00", за един студент само
  1012.                 }
  1013.                 finals.Add(pair.Key, infoPoPredmeti); // тук прибавяме студента, със съответната infoPoPredmeti
  1014.             }
  1015.  
  1016.             foreach (var pair in finals)
  1017.             {
  1018.                 string infoZaStudenta = string.Join(", ", pair.Value);
  1019.                 Console.WriteLine("{0}: [{1}]", pair.Key, infoZaStudenta);
  1020.             }
  1021.         }
  1022.     }
  1023. }
  1024.  
  1025.  
  1026. ///////////////////////////////////// Java Basics - 8 February 2015  //////////////////////////////////////////////////////////
  1027.  
  1028.  
  1029.  02. Letters Change Numbers  /////////////////////////////////////////////////////////////////
  1030.  
  1031.  
  1032. using System;
  1033. using System.Collections.Generic;
  1034. using System.Linq;
  1035. using System.Text;
  1036.  
  1037. namespace ConsoleApplication1
  1038. {
  1039.     class Program
  1040.     {
  1041.         static void Main(string[] args)
  1042.         {
  1043.            
  1044.             string[] input = Console.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  1045.  
  1046.             double sum = 0;
  1047.             for (int i = 0; i < input.Length; i++)
  1048.             {
  1049.                 string fromInput = input[i];
  1050.                 char before = fromInput[0];
  1051.                 char after = fromInput[fromInput.Length - 1];
  1052.                 double num = double.Parse(fromInput.Substring(1, fromInput.Length - 2));
  1053.  
  1054.                 if (char.IsUpper(before))
  1055.                 {
  1056.                     int beforePossition = before - 'A' + 1;
  1057.                     num /= beforePossition;
  1058.                 }
  1059.                 else
  1060.                 {
  1061.                     int beforePossition = before - 'a' + 1;
  1062.                     num *= beforePossition;
  1063.                 }
  1064.                 if (char.IsUpper(after))
  1065.                 {
  1066.                     int afrerPossition = after - 'A' + 1;
  1067.                     num -= afrerPossition;
  1068.                 }
  1069.                 else
  1070.                 {
  1071.                     int afrerPossition = after - 'a' + 1;
  1072.                     num += afrerPossition;
  1073.                 }
  1074.                 sum += num;
  1075.             }
  1076.             Console.WriteLine("{0:f2}", sum);
  1077.         }
  1078.     }
  1079. }
  1080.  
  1081.  
  1082. 04. User Logs /////////////////////////////////////////////////////////////////////////////////////
  1083.  
  1084.  
  1085. using System;
  1086. using System.Collections.Generic;
  1087. using System.Linq;
  1088. using System.Text;
  1089. using System.Threading.Tasks;
  1090.  
  1091. class Program
  1092. {
  1093.     static void Main(string[] args)
  1094.     {
  1095.         SortedDictionary<string, SortedDictionary<string, int>> dataDic =
  1096.                             new SortedDictionary<string, SortedDictionary<string, int>>();
  1097.  
  1098.         string input = Console.ReadLine();
  1099.         string[] inputTokens;
  1100.         string user;
  1101.         string ip;
  1102.         int counter;
  1103.  
  1104.         //Console.WriteLine("user = {0}", user);
  1105.         //Console.WriteLine("ip = {0}", ip);
  1106.  
  1107.         while (input != "end")
  1108.         {
  1109.             inputTokens = input.Split(new char[] { ' ', '=' }, StringSplitOptions.RemoveEmptyEntries);
  1110.             user = inputTokens[5];
  1111.             ip = inputTokens[1];
  1112.             counter = 1;
  1113.  
  1114.             if (!dataDic.ContainsKey(user))
  1115.             {
  1116.                 dataDic[user] = new SortedDictionary<string, int>();
  1117.             }
  1118.             if (!dataDic[user].ContainsKey(ip))
  1119.             {
  1120.                 dataDic[user][ip] = counter;
  1121.             }
  1122.             else
  1123.             {
  1124.                 dataDic[user][ip] += counter;
  1125.             }
  1126.  
  1127.             input = Console.ReadLine();
  1128.         }
  1129.         foreach (KeyValuePair<string, SortedDictionary<string, int>> entry in dataDic)
  1130.         {
  1131.             char ch = ',';
  1132.             Console.WriteLine(entry.Key + ": ");
  1133.             int i = 1;
  1134.             foreach (KeyValuePair<string, int> item in entry.Value)
  1135.             {
  1136.                 if (i == entry.Value.Count)
  1137.                 {
  1138.                     ch = '.';
  1139.                 }
  1140.                 Console.Write(item.Key + " => " + item.Value + ch + " ");
  1141.                 i++;
  1142.             }
  1143.             Console.WriteLine();
  1144.         }
  1145.  
  1146.         //foreach (KeyValuePair<string, SortedDictionary<string, int>> entry in dataDic)
  1147.         //{
  1148.         //    char ch = '.';
  1149.         //    Console.WriteLine(entry.Key + ":");
  1150.         //    foreach (KeyValuePair<string, int> item in entry.Value)
  1151.         //    {
  1152.         //        Console.WriteLine(item.Key + " => " + item.Value + ch);
  1153.         //    }
  1154.         //}
  1155.     }
  1156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement