Advertisement
abasar

Summer exercises

Aug 13th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.55 KB | None | 0 0
  1. //1
  2. A)
  3. |                        | num | output |
  4. |------------------------+-----+--------|
  5. |                        |  10 |        |
  6. |------------------------+-----+--------|
  7. | if (num<20)            |     |        |
  8. | while (num>2)          |     |        |
  9. | num -=2;               |   8 |        |
  10. |------------------------+-----+--------|
  11. | while (num>2)          |     |        |
  12. | num -=2;               |   6 |        |
  13. |------------------------+-----+--------|
  14. | while (num>2)          |     |        |
  15. | num -=2;               |   4 |        |
  16. |------------------------+-----+--------|
  17. | while (num>2)          |     |        |
  18. | num -=2;               |   2 |        |
  19. |------------------------+-----+--------|
  20. | if (num==2 || num==40) |     |        |
  21. | return 1;              |     |      1 |
  22. |------------------------+-----+--------|
  23. | return 0;              |     |        |
  24. |------------------------+-----+--------|
  25.  
  26. B)
  27. |                        | num | output |
  28. |------------------------+-----+--------|
  29. |                        |  33 |        |
  30. |------------------------+-----+--------|
  31. | else                   |     |        |
  32. | while (num<40)         |     |        |
  33. | num+=2;                |  35 |        |
  34. |------------------------+-----+--------|
  35. | while (num<40)         |     |        |
  36. | num+=2;                |  37 |        |
  37. |------------------------+-----+--------|
  38. | while (num<40)         |     |        |
  39. | num+=2;                |  39 |        |
  40. |------------------------+-----+--------|
  41. | while (num<40)         |     |        |
  42. | num+=2;                |  41 |        |
  43. |------------------------+-----+--------|
  44. | if (num==2 || num==40) |     |        |
  45. | return 1;              |     |        |
  46. |------------------------+-----+--------|
  47. | return 0;              |     |      0 |
  48. |------------------------+-----+--------|
  49.  
  50. //2
  51. static void Swap(string a)
  52.         {
  53.             if (a.Length > 100)
  54.             {
  55.                 Console.WriteLine("String length too long (100 characters max!)");
  56.             }
  57.             else
  58.             {
  59.                 if (a.Length % 2 == 1)
  60.                 {
  61.                     char last = a[a.Length - 1], first = a[0];
  62.                     string new_string = last + a.Substring(1, a.Length - 2) + first;
  63.                     Console.WriteLine(new_string);
  64.                 }
  65.                 else
  66.                 {
  67.                     char last = a[a.Length / 2], first = a[a.Length / 2 - 1];
  68.                     string new_string = a.Substring(0, a.Length / 2 - 1) + last + first + a.Substring(a.Length / 2 + 1);
  69.                     Console.WriteLine(new_string);
  70.                 }
  71.             }
  72.         }
  73.  
  74. //3 assuming the array is already defined and is named "a"
  75.             int max = 0, index = 0;
  76.             for(int i = 0; i < a.Length; i++)
  77.             {
  78.                 if(a[i] > max) {
  79.                     max = a[i];
  80.                     index = i;
  81.                 }
  82.             }
  83.             a[index] = a[a.Length - 1];
  84.             a[a.Length - 1] = max;
  85.  
  86. //4 assuming the array is already defined as is named "a"
  87.             bool last = false;                                   // true if the last character was a letter
  88.             for (int i = 0; i < a.Length; i++)  
  89.             {
  90.                 bool current = false;                           // true if this character is a letter
  91.                 for (char letter = 'a'; letter <= 'z'; letter++)
  92.                     if (!current)
  93.                         if (letter == a[i])
  94.                             current = true;
  95.                 if (current == false && last == true)
  96.                 {
  97.                     Console.Write("{0} ", i - 1);
  98.                 }
  99.                 last = current;
  100.             }
  101.  
  102. //5
  103. A )
  104. |                                   | A                  | T     | i | x | n | output |
  105. |-----------------------------------+--------------------+-------+---+---+---+--------|
  106. |                                   | [1, 2, 3, 4, 5, 6] | true  |   |   | 6 |        |
  107. |-----------------------------------+--------------------+-------+---+---+---+--------|
  108. | for (int i = 0; i <= n - 2; i++): |                    |       | 0 |   |   |        |
  109. | int x = A[i];                     |                    |       |   | 1 |   |        |
  110. | if (i != A[x]):                   |                    |       |   |   |   |        |
  111. | T = false;                        |                    | false |   |   |   |        |
  112. |-----------------------------------+--------------------+-------+---+---+---+--------|
  113. | for (int i = 0; i <= n - 2; i++): |                    |       | 1 |   |   |        |
  114. | int x = A[i];                     |                    |       |   | 2 |   |        |
  115. | if (i != A[x]):                   |                    |       |   |   |   |        |
  116. | T = false;                        |                    | false |   |   |   |        |
  117. |-----------------------------------+--------------------+-------+---+---+---+--------|
  118. | for (int i = 0; i <= n - 2; i++): |                    |       | 2 |   |   |        |
  119. | int x = A[i];                     |                    |       |   | 3 |   |        |
  120. | if (i != A[x]):                   |                    |       |   |   |   |        |
  121. | T = false;                        |                    | false |   |   |   |        |
  122. |-----------------------------------+--------------------+-------+---+---+---+--------|
  123. | for (int i = 0; i <= n - 2; i++): |                    |       | 3 |   |   |        |
  124. | int x = A[i];                     |                    |       |   | 4 |   |        |
  125. | if (i != A[x]):                   |                    |       |   |   |   |        |
  126. | T = false;                        |                    | false |   |   |   |        |
  127. |-----------------------------------+--------------------+-------+---+---+---+--------|
  128. | Console.WriteLine(T)              |                    |       |   |   |   | false  |
  129.  
  130. B) [4, 5, 3, 2, 0, 0]
  131.  
  132.  
  133. //6
  134.             int[] teachers = new int[100];
  135.             for (int i = 0; i < teachers.Length; i++)
  136.                 teachers[i] = 0;
  137.             for (int i = 0; i < 900; i++)
  138.             {
  139.                 Console.WriteLine("Please enter your two favorite teachers:");
  140.                 int first = int.Parse(Console.ReadLine()) - 1, second = int.Parse(Console.ReadLine()) - 1;
  141.                 while (second == first)
  142.                 {
  143.                     Console.WriteLine("Your can't pick the same number twice! please try again:");
  144.                     second = int.Parse(Console.ReadLine()) - 1;
  145.                 }
  146.                 teachers[first] += 1;
  147.                 teachers[second] += 1;
  148.             }
  149.             int max = 0, after_max = 0, index = 0;
  150.             Console.WriteLine("Teachers that aren't liked at all:");
  151.             for (int i = 0; i < teachers.Length; i++)
  152.                 if (teachers[i] == 0)
  153.                     Console.Write("{0} ", i + 1);
  154.             Console.WriteLine();
  155.             Console.WriteLine("The teacher that is liked the most:");
  156.             for (int i = 0; i < teachers.Length; i++)
  157.             {
  158.                 if (teachers[i] > max)
  159.                 {
  160.                     index = i + 1;
  161.                     max = teachers[i];
  162.                 }
  163.             }
  164.             Console.WriteLine(index);
  165.             Console.WriteLine("The teacher is the second most liked:");
  166.             for (int i = 0; i < teachers.Length; i++)
  167.             {
  168.                 if (teachers[i] > after_max && teachers[i] < max)
  169.                 {
  170.                     index = i + 1;
  171.                     max = teachers[i];
  172.                 }
  173.             }
  174.             Console.WriteLine(index);
  175.  
  176. //7
  177.         static void Check(string[] a)
  178.         {
  179.             bool symmetrical = true;
  180.             char last = a[0][a[0].Length - 1];
  181.             for (int i = 1; i < a.Length; i++)
  182.             {
  183.                 if (symmetrical)
  184.                 {
  185.                     if (a[i][0] != last)
  186.                     {
  187.                         symmetrical = false;
  188.                     }
  189.                     else
  190.                         last = a[i][a[i].Length - 1];
  191.                 }
  192.             }
  193.             if (symmetrical)
  194.                 Console.WriteLine("The array is symmetical!");
  195.             else
  196.                 Console.WriteLine("The array is not symmetrical!");
  197.         }
  198.  
  199. //8
  200.         static int Test8(int[] a, int k)
  201.         {
  202.             int sum = 0;
  203.             for (int i = k; i < a.Length; i++)
  204.                 sum += a[i];
  205.             return sum;
  206.         }
  207.         static void Main(string[] args)
  208.         {
  209.             int[] a = { 10, 5, 8, 7, 3 }; // just as an example
  210.             int n = a.Length;
  211.             int[] b = new int[n];
  212.             for (int i = 0; i < b.Length; i++)
  213.                 b[i] = Test8(a, i);
  214.         }
  215.  
  216. //9
  217.         static bool Tri(string s)
  218.         {
  219.             if (s.Length != 15)
  220.                 return false;
  221.             char[] chars = s.Substring(5, 5).ToCharArray();
  222.             Array.Reverse(chars);
  223.             string middle = new string(chars);
  224.             if (s.Substring(10, 5) == middle && s.Substring(0, 5) == middle)
  225.                 return true;
  226.             return false;
  227.         }
  228.         static void Main(string[] args)
  229.         {
  230.             string[] a = new string[10];
  231.             int sum = 0;
  232.             for (int i = 0; i< a.Length; i++)
  233.             {
  234.                 a[i] = Console.ReadLine();
  235.                 if (Tri(a[i]))
  236.                     sum++;
  237.             }
  238.             //sum will be the ammount of mahrozot meshulashot
  239. //10
  240.         static int DaysPassed(int[] year, int day, int month)
  241.         {
  242.             int daysPassed = 0;
  243.             for (int i = 0; i < month - 1; i++)
  244.                 daysPassed += year[i];
  245.             return daysPassed + day;
  246.         }
  247.         static void Main(string[] args)
  248.         {
  249.             int[] year = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  250.             int day = int.Parse(Console.ReadLine()), month = int.Parse(Console.ReadLine());
  251.             while (day != 0 && month != 0)
  252.             {
  253.                 if (DaysPassed(year, day, month) > 60)
  254.                     Console.WriteLine("The date is too far.");
  255.                 else if (DaysPassed(year, day, month) < 10)
  256.                     Console.WriteLine("The date is too close.");
  257.                 else
  258.                     Console.WriteLine("The date is good.");
  259.                 day = int.Parse(Console.ReadLine());
  260.                 month = int.Parse(Console.ReadLine());
  261.             }
  262.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement