Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //1
- A)
- | | num | output |
- |------------------------+-----+--------|
- | | 10 | |
- |------------------------+-----+--------|
- | if (num<20) | | |
- | while (num>2) | | |
- | num -=2; | 8 | |
- |------------------------+-----+--------|
- | while (num>2) | | |
- | num -=2; | 6 | |
- |------------------------+-----+--------|
- | while (num>2) | | |
- | num -=2; | 4 | |
- |------------------------+-----+--------|
- | while (num>2) | | |
- | num -=2; | 2 | |
- |------------------------+-----+--------|
- | if (num==2 || num==40) | | |
- | return 1; | | 1 |
- |------------------------+-----+--------|
- | return 0; | | |
- |------------------------+-----+--------|
- B)
- | | num | output |
- |------------------------+-----+--------|
- | | 33 | |
- |------------------------+-----+--------|
- | else | | |
- | while (num<40) | | |
- | num+=2; | 35 | |
- |------------------------+-----+--------|
- | while (num<40) | | |
- | num+=2; | 37 | |
- |------------------------+-----+--------|
- | while (num<40) | | |
- | num+=2; | 39 | |
- |------------------------+-----+--------|
- | while (num<40) | | |
- | num+=2; | 41 | |
- |------------------------+-----+--------|
- | if (num==2 || num==40) | | |
- | return 1; | | |
- |------------------------+-----+--------|
- | return 0; | | 0 |
- |------------------------+-----+--------|
- //2
- static void Swap(string a)
- {
- if (a.Length > 100)
- {
- Console.WriteLine("String length too long (100 characters max!)");
- }
- else
- {
- if (a.Length % 2 == 1)
- {
- char last = a[a.Length - 1], first = a[0];
- string new_string = last + a.Substring(1, a.Length - 2) + first;
- Console.WriteLine(new_string);
- }
- else
- {
- char last = a[a.Length / 2], first = a[a.Length / 2 - 1];
- string new_string = a.Substring(0, a.Length / 2 - 1) + last + first + a.Substring(a.Length / 2 + 1);
- Console.WriteLine(new_string);
- }
- }
- }
- //3 assuming the array is already defined and is named "a"
- int max = 0, index = 0;
- for(int i = 0; i < a.Length; i++)
- {
- if(a[i] > max) {
- max = a[i];
- index = i;
- }
- }
- a[index] = a[a.Length - 1];
- a[a.Length - 1] = max;
- //4 assuming the array is already defined as is named "a"
- bool last = false; // true if the last character was a letter
- for (int i = 0; i < a.Length; i++)
- {
- bool current = false; // true if this character is a letter
- for (char letter = 'a'; letter <= 'z'; letter++)
- if (!current)
- if (letter == a[i])
- current = true;
- if (current == false && last == true)
- {
- Console.Write("{0} ", i - 1);
- }
- last = current;
- }
- //5
- A )
- | | A | T | i | x | n | output |
- |-----------------------------------+--------------------+-------+---+---+---+--------|
- | | [1, 2, 3, 4, 5, 6] | true | | | 6 | |
- |-----------------------------------+--------------------+-------+---+---+---+--------|
- | for (int i = 0; i <= n - 2; i++): | | | 0 | | | |
- | int x = A[i]; | | | | 1 | | |
- | if (i != A[x]): | | | | | | |
- | T = false; | | false | | | | |
- |-----------------------------------+--------------------+-------+---+---+---+--------|
- | for (int i = 0; i <= n - 2; i++): | | | 1 | | | |
- | int x = A[i]; | | | | 2 | | |
- | if (i != A[x]): | | | | | | |
- | T = false; | | false | | | | |
- |-----------------------------------+--------------------+-------+---+---+---+--------|
- | for (int i = 0; i <= n - 2; i++): | | | 2 | | | |
- | int x = A[i]; | | | | 3 | | |
- | if (i != A[x]): | | | | | | |
- | T = false; | | false | | | | |
- |-----------------------------------+--------------------+-------+---+---+---+--------|
- | for (int i = 0; i <= n - 2; i++): | | | 3 | | | |
- | int x = A[i]; | | | | 4 | | |
- | if (i != A[x]): | | | | | | |
- | T = false; | | false | | | | |
- |-----------------------------------+--------------------+-------+---+---+---+--------|
- | Console.WriteLine(T) | | | | | | false |
- B) [4, 5, 3, 2, 0, 0]
- //6
- int[] teachers = new int[100];
- for (int i = 0; i < teachers.Length; i++)
- teachers[i] = 0;
- for (int i = 0; i < 900; i++)
- {
- Console.WriteLine("Please enter your two favorite teachers:");
- int first = int.Parse(Console.ReadLine()) - 1, second = int.Parse(Console.ReadLine()) - 1;
- while (second == first)
- {
- Console.WriteLine("Your can't pick the same number twice! please try again:");
- second = int.Parse(Console.ReadLine()) - 1;
- }
- teachers[first] += 1;
- teachers[second] += 1;
- }
- int max = 0, after_max = 0, index = 0;
- Console.WriteLine("Teachers that aren't liked at all:");
- for (int i = 0; i < teachers.Length; i++)
- if (teachers[i] == 0)
- Console.Write("{0} ", i + 1);
- Console.WriteLine();
- Console.WriteLine("The teacher that is liked the most:");
- for (int i = 0; i < teachers.Length; i++)
- {
- if (teachers[i] > max)
- {
- index = i + 1;
- max = teachers[i];
- }
- }
- Console.WriteLine(index);
- Console.WriteLine("The teacher is the second most liked:");
- for (int i = 0; i < teachers.Length; i++)
- {
- if (teachers[i] > after_max && teachers[i] < max)
- {
- index = i + 1;
- max = teachers[i];
- }
- }
- Console.WriteLine(index);
- //7
- static void Check(string[] a)
- {
- bool symmetrical = true;
- char last = a[0][a[0].Length - 1];
- for (int i = 1; i < a.Length; i++)
- {
- if (symmetrical)
- {
- if (a[i][0] != last)
- {
- symmetrical = false;
- }
- else
- last = a[i][a[i].Length - 1];
- }
- }
- if (symmetrical)
- Console.WriteLine("The array is symmetical!");
- else
- Console.WriteLine("The array is not symmetrical!");
- }
- //8
- static int Test8(int[] a, int k)
- {
- int sum = 0;
- for (int i = k; i < a.Length; i++)
- sum += a[i];
- return sum;
- }
- static void Main(string[] args)
- {
- int[] a = { 10, 5, 8, 7, 3 }; // just as an example
- int n = a.Length;
- int[] b = new int[n];
- for (int i = 0; i < b.Length; i++)
- b[i] = Test8(a, i);
- }
- //9
- static bool Tri(string s)
- {
- if (s.Length != 15)
- return false;
- char[] chars = s.Substring(5, 5).ToCharArray();
- Array.Reverse(chars);
- string middle = new string(chars);
- if (s.Substring(10, 5) == middle && s.Substring(0, 5) == middle)
- return true;
- return false;
- }
- static void Main(string[] args)
- {
- string[] a = new string[10];
- int sum = 0;
- for (int i = 0; i< a.Length; i++)
- {
- a[i] = Console.ReadLine();
- if (Tri(a[i]))
- sum++;
- }
- //sum will be the ammount of mahrozot meshulashot
- //10
- static int DaysPassed(int[] year, int day, int month)
- {
- int daysPassed = 0;
- for (int i = 0; i < month - 1; i++)
- daysPassed += year[i];
- return daysPassed + day;
- }
- static void Main(string[] args)
- {
- int[] year = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- int day = int.Parse(Console.ReadLine()), month = int.Parse(Console.ReadLine());
- while (day != 0 && month != 0)
- {
- if (DaysPassed(year, day, month) > 60)
- Console.WriteLine("The date is too far.");
- else if (DaysPassed(year, day, month) < 10)
- Console.WriteLine("The date is too close.");
- else
- Console.WriteLine("The date is good.");
- day = int.Parse(Console.ReadLine());
- month = int.Parse(Console.ReadLine());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement