C# – Practical Exams_Part 1

Условия и решения на задачите за подготовка за практически изпит по C#

Условия и решения на задачите от тест Вариант 1

Задача 1. Problem-Ship Damage

Условие:
Inside the sea (a standard Cartesian /rectangular/ coordinate system) we are given a ship S (a rectangle whose sides are parallel to the coordinate axes), a horizontal line H (the horizon) and three catapults, given as coordinates C1, C2 and C3 that will be used to fire the ship. When the attack starts, each catapult hits a projectile exactly into the positions that are symmetrical to C1, C2 and C3 with respect to the horizon H. When a projectile hits some of the corners of the ship, it causes a damage of 25%, when it hits some of the sides of the ship, the damage caused is 50% and when it hits the internal body of the ship, the damage is 100%. When the projectile hit outside of the ship, there is no damage. The total damage is sum of the separate damages and can exceed 100%.
At the figure below a sea, a ship S, a line H, three points C1, C2 and C3 and their hit positions are shown:
Practical exam_CSharp_Variant 1_a

Your task is to write a program that calculates the total damage caused after the attack over the ship.

Input
The input data should be read from the console.
There will be exactly 11 lines holding the integer numbers SX1, SY1, SX2, SY2, H, CX1, CY1, CX2, CY2, CX3, and CY3. The ship S is given by any two of its opposite corners and is non-empty (has positive width and height). The line H is given by its vertical offset. The points C1, C2 and C3 are given as couples of coordinates and cannot overap each other.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output data should be printed on the console.
The output should consist of a single line holding the total damage given as percentage.

Constraints
• The numbers SX1, SY1, SX2, SY2, H, CX1, CY1, CX2, CY2, CX3, and CY3 are all integers between -100 000 and 100 000, inclusive.
• Allowed work time for your program: 0.1 seconds.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Variant 1_b

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _1.Problem_ShipDamage
{
class ShipDamage
{
static void Main()
{
int Sx1 = int.Parse(Console.ReadLine());
int Sy1 = int.Parse(Console.ReadLine());
int Sx2 = int.Parse(Console.ReadLine());
int Sy2 = int.Parse(Console.ReadLine());
int H = int.Parse(Console.ReadLine());
int Cx1 = int.Parse(Console.ReadLine());
int Cy1 = int.Parse(Console.ReadLine());
int Cx2 = int.Parse(Console.ReadLine());
int Cy2 = int.Parse(Console.ReadLine());
int Cx3 = int.Parse(Console.ReadLine());
int Cy3 = int.Parse(Console.ReadLine());

int newCy1 = H + (H - Cy1);
int newCy2 = H + (H - Cy2);
int newCy3 = H + (H - Cy3);
double damage = 0;

//Corners hit
if ((Cx1==Sx1 && newCy1==Sy1)||(Cx1==Sx2 && newCy1==Sy2)||(Cx1==Sx1 && newCy1==Sy2)||(Cx1==Sx2 && newCy1==Sy1))
{
damage=damage+0.25;
}
if ((Cx2==Sx1 && newCy2==Sy1)||(Cx2==Sx2 && newCy2==Sy2)||(Cx2==Sx1 && newCy2==Sy2)||(Cx2==Sx2 && newCy2==Sy1))
{
damage=damage+0.25;
}
if ((Cx3==Sx1 && newCy3==Sy1)||(Cx3==Sx2 && newCy3==Sy2)||(Cx3==Sx1 && newCy3==Sy2)||(Cx3==Sx2 && newCy3==Sy1))
{
damage=damage+0.25;
}

//Sides hit
if ((Cx1==Sx1 && newCy1 < Math.Max(Sy1, Sy2) && newCy1 > Math.Min(Sy1, Sy2))||
(Cx1==Sx2 && newCy1 < Math.Max(Sy1, Sy2) && newCy1 > Math.Min(Sy1, Sy2))||
(newCy1==Sy1 && Cx1 < Math.Max(Sx1, Sx2) && Cx1 > Math.Min(Sx1, Sx2))||
(newCy1==Sy2 && Cx1 < Math.Max(Sx1, Sx2) && Cx1 > Math.Min(Sx1, Sx2)))
{
damage=damage+0.5;
}
if ((Cx2==Sx1 && newCy2 < Math.Max(Sy1, Sy2) && newCy2 > Math.Min(Sy1, Sy2))||
(Cx2==Sx2 && newCy2 < Math.Max(Sy1, Sy2) && newCy2 > Math.Min(Sy1, Sy2))||
(newCy2==Sy1 && Cx2 < Math.Max(Sx1, Sx2) && Cx2 > Math.Min(Sx1, Sx2))||
(newCy2==Sy2 && Cx2 < Math.Max(Sx1, Sx2) && Cx2 > Math.Min(Sx1, Sx2)))
{
damage=damage+0.5;
}
if ((Cx3==Sx1 && newCy3 < Math.Max(Sy1, Sy2) && newCy3 > Math.Min(Sy1, Sy2))||
(Cx3==Sx2 && newCy3 < Math.Max(Sy1, Sy2) && newCy3 > Math.Min(Sy1, Sy2))||
(newCy3==Sy1 && Cx3 < Math.Max(Sx1, Sx2) && Cx3 > Math.Min(Sx1, Sx2))||
(newCy3==Sy2 && Cx3 < Math.Max(Sx1, Sx2) && Cx3 > Math.Min(Sx1, Sx2)))
{
damage=damage+0.5;
}

//Body hit
if ((Cx1 < Math.Max(Sx1, Sx2) && Cx1 > Math.Min(Sx1, Sx2) && newCy1 < Math.Max(Sy1, Sy2) && newCy1 > Math.Min(Sy1, Sy2)))
{
damage=damage+1;
}
if ((Cx2 < Math.Max(Sx1, Sx2) && Cx2 > Math.Min(Sx1, Sx2) && newCy2 < Math.Max(Sy1, Sy2) && newCy2 > Math.Min(Sy1, Sy2)))
{
damage=damage+1;
}
if ((Cx3 < Math.Max(Sx1, Sx2) && Cx3 > Math.Min(Sx1, Sx2) && newCy3 < Math.Max(Sy1, Sy2) && newCy3 > Math.Min(Sy1, Sy2)))
{
damage=damage+1;
}
Console.WriteLine("{0:0%}",damage);
}
}
}

Задача 2. Problem-Tribonacci

Условие:
The Tribonacci sequence is a sequence in which every next element is made by the sum of the previous three elements from the sequence.

Write a computer program that finds the Nth element of the Tribonacci sequence, if you are given the first three elements of the sequence and the number N. Mathematically said: with given T1, T2 and T3 – you must find Tn.

Input
The input data should be read from the console.
The values of the first three Tribonacci elements will be given on the first three input lines.
The number N will be on the fourth line. This is the number of the consecutive element of the sequence that must be found by your program.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output data should be printed on the console.
At the only output line you must print the Nth element of the given Tribonacci sequence.

Constraints
• The values of the first three elements of the sequence will be integers between -2 000 000 000 and 2 000 000 000.
• The number N will be a positive integer between 1 and 15 000, inclusive.
• Allowed working time for your program: 0.25 seconds.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Variant 1_c

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;

namespace _2.Problem_Tribonacci
{
class Tribonacci
{
static void Main()
{

BigInteger T1 = BigInteger.Parse(Console.ReadLine());
BigInteger T2 = BigInteger.Parse(Console.ReadLine());
BigInteger T3 = BigInteger.Parse(Console.ReadLine());
BigInteger[] sequenceMembers = { T1, T2, T3 };
int N = int.Parse(Console.ReadLine());
BigInteger newElement = 0;
switch (N)
{
case 1: newElement = T1; break;
case 2: newElement = T2; break;
case 3: newElement = T3; break;
default: break;
}
for (int i = 0; i < N - 3; i++)
{
newElement = sequenceMembers[0] + sequenceMembers[1] + sequenceMembers[2];
sequenceMembers[0] = sequenceMembers[1];
sequenceMembers[1] = sequenceMembers[2];
sequenceMembers[2] = newElement;
}
Console.WriteLine(newElement);
}
}
}

Задача 3. Problem-Fir Tree

Условие:
Christmas Eve is coming so even programmers got to prepare!
In the spirit of the event your task is to write a program that prints a fir tree to the console.
The format of the tree is shown in the examples bellow.

Input
The input data should be read from the console.
On the only input line you have an integer number N, showing the height of the tree.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output data should be printed on the console.
You must print the fir tree on the console. Each row contains only characters „.“ (point) or „*“ (asterisk).
The first row should have exactly one „*“ in the middle (that is the top of the tree) and each of the next lines two more.
The last line should have exactly one asterisk in the middle, showing the stem of the tree.

Constraints
• The number N is a positive integer between 4 and 100, inclusive.
• Allowed working time for your program: 0.25 seconds.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Variant 1_d

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _3.Problem_FirTree
{
class FirTree
{
static void Main()
{
int N = int.Parse(Console.ReadLine());
int firTreeWidth = N + (N - 4) + 1;
char[] line= new char[firTreeWidth];
char[] lastLine = new char[firTreeWidth];
for (int i = 0; i < firTreeWidth; i++)
{
line[i] = '.';
lastLine[i] = '.';
}
lastLine[firTreeWidth / 2] = '*';
int branchWidth = 0;
for (int i = 0; i < N-1; i++)
{
for (int j = 0; j < firTreeWidth; j++)
{
if ((j >= firTreeWidth / 2 - branchWidth) && (j <= firTreeWidth / 2 + branchWidth))
{
line[j] = '*';
}
Console.Write(line[j]);
}
branchWidth++;
Console.WriteLine();
}
for (int i = 0; i < firTreeWidth; i++)
{
Console.Write(lastLine[i]);
}
Console.WriteLine();
}
}
}

Задача 4. Problem-We All Love Bits

Условие:
One of the things the programmers love the most is bitwise operations. The „bitwise guy“ is a synonym for a programmer that loves bits more than everything else in programming. Mitko is a „bitwise guy“. He invented a new bitwise algorithm. The algorithm takes one positive integer number P, makes magic with it and returns a new positive integer number. He also defined a new number P̃ which represents the number P in binary numeral system with inverted bits. All zeros in P are ones in P̃ and all ones in P are zeros in P̃. For example if we have P = 9 (which is 1001 in binary numeral system) its inverted number P̃ will be equal to 6 (which is 110 in binary numeral system). But that’s not all! He invented another number P̈, which represents reversed number P in binary numeral system. For example if we have P = 11 (which is 1011 in binary numeral system) its reversed number P̈ is equal to 13 (which is 1101 in binary numeral system). The Mitko’s magical algorithm takes a number P and transforms it to a new number Pnew using the following bitwise transformation: Pnew = (P ^ P̃) & P̈.
Your task is to write a program that transforms a sequence of N positive integer numbers using Mitko’s algorithm.

Input
The input data should be read from the console.
At the first input line there will be one positive integer – the number N.
At each of the next N lines there will be one positive integer – the consequent number that must be converted using Mitko’s algorithm.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output data should be printed on the console.
The output must consist of N lines, containing the transformed numbers for each number from the input.

Constraints
• The number N will be positive integer number between 1 and 20 000, inclusive.
• Each of the N numbers will be positive integer numbers between 1 and 2 147 483 647, inclusive.
• Allowed working time for your program: 0.20 seconds.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Variant 1_e

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _4.Problem_WeAllLoveBits
{
class WeAllLoveBits
{
static void Main()
{
uint N = uint.Parse(Console.ReadLine());
uint[] numbers = new uint[N];
for (int i = 0; i < N; i++)
{
numbers[i] = uint.Parse(Console.ReadLine());
}

for (int j = 0; j < N; j++)
{
uint invertedP = ~ numbers[j];
uint reversedP = 0;
uint Pnew = 0;
int numberBitLength=0;
uint currentNumber=numbers[j];

//find the length of the number in bits
for (int i = 31; i >= 0; i--)
{
int mask = 1 << i;
if ((numbers[j] & mask) > 0)
{
numberBitLength = i;
break;
}
}
numberBitLength++;

//find reverse number
for (uint i = 0; i < 32; ++i)
{
reversedP = reversedP << 1;
reversedP = reversedP | (currentNumber & 1);
currentNumber = currentNumber >> 1;
}

reversedP = reversedP >> (32 - numberBitLength);

Pnew = (numbers[j] ^ invertedP) & reversedP;
Console.WriteLine(Pnew);
}
}
}
}

Задача 5. Problem-Pillars

Условие:
You are given a list of 8 bytes (positive integers in the range [0…255]) n0, n1, …, n7. These numbers represent a square grid consisting of 8 lines and 8 columns. Each cell of the grid could either be empty or full. The first line is represented by the bits of n0, the second – by the bits of n1 and so on, and the last line is represented by the bits of n7. Each bit with value 1 denotes a full cell and each bit with value 0 denotes an empty cell. The lines are numbered from the first (top) to the last (bottom) with the numbers 0, 1, …, 7. The columns are numbered from right to left with the indices 0, 1, …, 7. The figure shows a square grid and its representation by a sequence of 8 numbers n0, n1, …, n7:
Practical exam_CSharp_Variant 1_f

We are allowed to put a vertical pillar over any of the columns in the grid. Pillars split the grid into two sides (left and right) and the column holding the pillar is ignored. Write a program that finds the leftmost column where the pillar can be put so that the full cells on the left side and on the right side are equal number. For example at the figure if we put the pillar at column 5, it will split the grid into two sides and both sides will have exactly 3 full cells.

Input
The input data should be read from the console.
There will be exactly 8 lines each holding the integer numbers n0, n1, …, n7.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output data should be printed on the console.
If a pillar splitting the grid into two vertical sides each holding the same number of full cells exists, print its column index on the first line and the number of full cells in each of the sides. If multiple pillars can do the job, print only the leftmost. If no such pillar exists, print the string „No“ on the console (just one line holding the word „No“).

Constraints
• The numbers n0, n1, …, n7 are positive integers in the range [0…255].
• Allowed work time for your program: 0.25 seconds.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Variant 1_g

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _5.Problem_Pillars
{
class Pillars
{
static void Main()
{
byte[] bytes = new byte[8];
byte[] numberOfBitsOnPosition = new byte[8];
for (int i = 0; i < 8; i++)
{
bytes[i] = byte.Parse(Console.ReadLine());
}

for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if ((bytes[i] & (1 << j)) != 0)
{
numberOfBitsOnPosition[j]++;
}
}
}

int leftSum=0;
int rigthSum=0;
int endSum=0;
int pillarPosition=0;
bool isPillarPresent = false;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < i; j++)
{
leftSum=leftSum+numberOfBitsOnPosition[j];
}
for (int j = i+1; j < 8; j++)
{
rigthSum=rigthSum+numberOfBitsOnPosition[j];
}
if (leftSum==rigthSum)
{
endSum = leftSum;
pillarPosition = i;
isPillarPresent = true;
}
leftSum = 0;
rigthSum = 0;
}
if (isPillarPresent)
{
Console.WriteLine(pillarPosition);
Console.WriteLine(endSum);
}
else
{
Console.WriteLine("No");
}
}
}
}

Условия и решения на задачите от тест Вариант 2

Задача 1. Problem-Fighter Attack

Условие:
A rectangular plant P located in east-west direction is under attack by a fighter aircraft flying over it on the west. When the fighter launches a missile we have its coordinates F. It is assumed that the missile’s direction is always straight on the west and the missile always hits the target after a fixed distance D in front of the fighter.
To simplify our model we assume the land is built of square cells of size 1 x 1 located in east-west direction and each cell has integer Cartesian coordinates {x, y}. In this model the plant can be represented by a rectangular area of cell and the missile always hits some of the square cells (inside or outside of the plant).
When the missile hits a certain cell, the damage over it is 100%, on the cells staying on the left and on the right of it the damage is 50% and in front of it the damage is 75%. The total damage is sum of the separate damages and can exceed 100%.
You are given the location of the plant P, the location of the fighter F and the distance D. Write a program that calculates the damage over the plant after the attack. Not that the missile could hits the plant partially of fully or can hit some area outside of the plant and cause no damage.
At the figure below a plant P, a fighter F, a distance D and the missile hit point are shown along with the damage caused over the cells by the hit. Note that some of the damaged cells are outside of the plant and thus the total damage is 225%:
Practical exam_CSharp_Variant 2_a

Your task is to write a program that calculates the total damage caused after the attack over the plant.

Input
The input data should be read from the console. There will be exactly 7 lines holding the integer numbers PX1, PY1, PX2, PY2, FX, FY, and D. The plant P is given by the coordinates of any two of its opposite corners and is non-empty (consists of at least one cell). The location of the fighter is given as cell coordinates FX, and FY and the distance D is given as an integer number.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output should consist of a single line holding the total damage given as percentage.

Constraints
• The numbers PX1, PY1, PX2, PY2, FX, FY, and D are all integers in the range [-100 000 … 100 000].
• Allowed work time for your program: 1 second.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Variant 2_b

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _1.Problem_FighterAttack
{
class FighterAttack
{
static void Main()
{
int Px1 = int.Parse(Console.ReadLine());
int Py1 = int.Parse(Console.ReadLine());
int Px2 = int.Parse(Console.ReadLine());
int Py2 = int.Parse(Console.ReadLine());
int Fx = int.Parse(Console.ReadLine());
int Fy = int.Parse(Console.ReadLine());
int D = int.Parse(Console.ReadLine());
double damage = 0;
int newFx = Fx + D;

//100% hit
if ((newFx <= Math.Max(Px1, Px2) && newFx >= Math.Min(Px1, Px2) && Fy <= Math.Max(Py1, Py2) && Fy >= Math.Min(Py1, Py2)))
{
damage = damage + 1;
}

//Up 50% hit
if ((newFx <= Math.Max(Px1, Px2) && newFx >= Math.Min(Px1, Px2) && Fy + 1 <= Math.Max(Py1, Py2) && Fy + 1 >= Math.Min(Py1, Py2)))
{
damage = damage + 0.5;
}

//Down 50% hit
if ((newFx <= Math.Max(Px1, Px2) && newFx >= Math.Min(Px1, Px2) && Fy - 1 <= Math.Max(Py1, Py2) && Fy - 1 >= Math.Min(Py1, Py2)))
{
damage = damage + 0.5;
}

//Front 75% hit
if ((newFx + 1 <= Math.Max(Px1, Px2) && newFx + 1 >= Math.Min(Px1, Px2) && Fy <= Math.Max(Py1, Py2) && Fy >= Math.Min(Py1, Py2)))
{
damage = damage + 0.75;
}
Console.WriteLine("{0:0%}", damage);
}
}
}

Задача 2. Problem-Astrological Digits

Условие:
The astrological digit of a given number N is a digit calculated by the number’s digits by a special algorithm. The algorithm performs the following steps:
(1) Sums the digits of the number N and stores the result back in N.
(2) If the obtained result is bigger than 9, step (1) is repeated, otherwise the algorithm finishes.
The last obtained value of N is the result, calculated by the algorithm.

Input
The input data should be read from the console.
The only line in the input contains a number N, which can be integer or real number (decimal fraction).
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output data should be printed on the console.
You must print the calculated astrological digit of the number N on the first and only line of the output.

Constraints
• The number N will be in range [-1.7 × 10−308… 1.7 × 10308]. It will have no more than 300 digits before and after the decimal point.
• The decimal separator will always be the „.“ symbol.
• Allowed working time for your program: 0.25 seconds.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Variant 2_c

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _2.Problem_AstrologicalDigits
{
class AstrologicalDigits
{
static void Main()
{
string N = Console.ReadLine();
int newN=0;
int sumNdigits = 0;
char[] digits = N.ToCharArray();

while(true)
{
while (true)
{
for (int i = 0; i < digits.Length; i++)
{
if (digits[i] != '-' && digits[i] != '.')
{
int result=0;
bool isParsePossible = int.TryParse(Convert.ToString(digits[i]), out result);
if (isParsePossible)
{
sumNdigits = sumNdigits + result;
}
}
}
if (sumNdigits <= 9)
{
Console.WriteLine(sumNdigits);
return;
}
else
{
newN = sumNdigits;
sumNdigits = 0;
digits = (Convert.ToString(newN)).ToCharArray();
break;
}
}
}
}
}
}

Задача 3. Problem-Sand Glass

Условие:
Once upon a time a powerful wizard was born. His name was Gwenogfryn and soon he became a great sorcerer. Kind-hearted he was. He would only use his magic to protect humans from the evil witches that would come at night. Gwenogfryn, however was a pacifist and did not want to fight or hurt the witches, so he came up with another solution. He would catch the witches and throw them into a sand-glass (the only prison a witch cannot escape from). Unfortunately, he is running out of sand-glasses. Help Gwenogfryn catch all witches by making your own sand-glasses.

Input
The input data should be read from the console.
You have an integer number N (always odd number) showing the height of the sand clock.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output should be printed on the console.
You should print the hourglass on the console. Each row can contain only the following characters: “.” (dot) and “*” (asterisk). As shown in the example: the middle row must contain only one ‘*’ and all other symbols must be “.”. Every next row (up or down from the middle one) must contain the same number of ‘*’ as the previous one plus two. You should only use “.” to fill-in the rows, where necessary.

Constraints
• The number N will be a positive integer number between 3 and 101, inclusive.
• Allowed working time for your program: 0.25 seconds.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Variant 2_d

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _3.Problem_SandGlass
{
class SandGlass
{
static void Main()
{
int N = int.Parse(Console.ReadLine());
char[] line = new char[N];
int sandWidth = 0;
for (int i = 0; i < N; i++)
{
line[i] = '*';
}

for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (j < sandWidth || j > N - sandWidth -1)
{
Console.Write(".");
}
else
{
Console.Write(line[j]);
}
}
Console.WriteLine();
if (i < N / 2)
{
sandWidth++;
}
else
{
sandWidth--;
}
}
}
}
}

Задача 4. Problem-Dancing Bits

Условие:
Gergana loves dancing and she also likes bits (she doesn’t know what bits really are, but she knows that she likes them). Few days ago she accidently invented a new term – “dancing bits”.
If you ask her what “dancing bits” mean she will tell you that it’s a sequence of identical bits (so the bits can dance together – zeros can only dance with other zeros, the same applies for ones).
You are given N positive integer numbers that are converted to binary numeral system and are concatenated together in one big sequence of bits.
For example: if we have 4 numbers: 5 (101 in binary numeral system), 6 (110 in binary numeral system), 14 (1110 in binary numeral system) and 143 (1000111 in binary numeral system) their concatenation will be 101110111010001111.
You are also given a positive integer K – the number of identical bits (zeroes or ones that can dance together).
Write a program that finds the number of all “dancing bits” (the sequences of equal bits) with a length of exactly K bits. Your program should search in the concatenation of the given N numbers.
For example, if we have 4 numbers (5, 6, 14 and 143, the concatenation of their binary representation is 101110111010001111) and we are searching for the total number of all sequences of equal bits with an exact length of 3 bits, the answer will be 3 (the sequences are bolded in the concatenation above).
In this example we have two sequences of “dancing bits” – „111“ consisting of only ones and one sequence of “dancing bits” – „000“ consisting of only zeros. Note that the sequence „1111“ is not a sequence of exact 3 identical bits.

Input
The input data should be read from the console.
At the first input line there will be one positive integer – the number K.
At the second input line there will be another positive integer – the number N.
At each of the next N lines there will be one positive integer – the N numbers that represent the input sequence of bits.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output data should be printed on the console.
The only output line must contain the answer – the number of “dancing bits” sequences found.

Constraints
• The number K will be positive integer number between 1 and 25 600, inclusive.
• The number N will be positive integer number between 1 and 800, inclusive.
• Each of the N numbers will be positive integer numbers between 1 and 2 147 483 647, inclusive.
• Allowed working time for your program: 0.20 seconds.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Variant 2_e

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _4.Problem_DancingBits
{
class DancingBits
{
static void Main()
{
int K = int.Parse(Console.ReadLine());
int N = int.Parse(Console.ReadLine());
int[] numbers = new int[N];
string concatenatedNumbers=null;
char[] bitsAsChar;
int dancingBitsCounter = 1;
int numberOfDancingBitsGroups=0;
for (int i = 0; i < N; i++)
{
numbers[i] = int.Parse(Console.ReadLine());
}

for (int i = 0; i < N; i++)
{
concatenatedNumbers = concatenatedNumbers + Convert.ToString(numbers[i], 2);
}

bitsAsChar = concatenatedNumbers.ToCharArray();

for (int i = 1; i < bitsAsChar.Length; i++)
{
if (bitsAsChar[i] == bitsAsChar[i - 1])
{
dancingBitsCounter++;
}
else
{
if (dancingBitsCounter==K)
{
numberOfDancingBitsGroups++;
}
dancingBitsCounter = 1;
}
if (i==(bitsAsChar.Length-1))//Last bit
{
if (dancingBitsCounter == K)
{
numberOfDancingBitsGroups++;
}
}
}
Console.WriteLine(numberOfDancingBitsGroups);
}
}
}

Задача 5. Problem-Lines

Условие:
You are given a list of 8 bytes (positive integers in the range [0…255]) n0, n1, …, n7. These numbers represent a square grid consisting of 8 lines and 8 columns. Each cell of the grid could either be empty or full. The first line is represented by the bits of n0, the second – by the bits of n1 and so on, and the last line is represented by the bits of n7. Each bit with value 1 denotes a full cell and each bit with value 0 denotes an empty cell. The lines are numbered from the first (top) to the last (bottom) with the numbers 0, 1, …, 7. The columns are numbered from right to left with the indices 0, 1, …, 7. The figure shows a sample square grid and its representation by a sequence of 8 numbers n0, n1, …, n7:
Practical exam_CSharp_Variant 2_f

A line is any sequence of full cells staying on the same row or column. At the figure above we have two lines of 4 cells and two lines of 3 cells and one line of 1 cell. You need to create a program that finds the longest line in the grid and the number of lines with the longest length. At the figure we have two largest lines with length of 4 cells.

Input
The input data is should be read from the console. There will be exactly 8 lines each holding the integer numbers n0, n1, …, n7. It is guaranteed that there exists at least one line in the grid (the grid is not empty).
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output consists of two integers placed on separate lines. The first line should hold the length of the longest line in the grid. The second line should hold the number of lines with the maximal length.

Constraints
• The numbers n0, n1, …, n7 are positive integers in the range [0…255].
• Allowed work time for your program: 0.25 seconds.
• Allowed memory: 16 MB.

Example
Practical exam_CSharp_Variant 2_g

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _5.Problem_Lines
{
class Lines
{
static void Main()
{
int N = 8;
int[] numbers = new int[N];
string numberAsString;
char[][] numbersGrid = new char[8][];
int currentLineLength = 1;
int longestLineLength = 1;
int numberOfLongestLines=1;

for (int i = 0; i < N; i++)
{
numbers[i] = int.Parse(Console.ReadLine());
numberAsString = Convert.ToString(numbers[i], 2).PadLeft(8,'0');
numbersGrid[i]=numberAsString.ToCharArray();
}

//Check rows
for (int rows = 0; rows < N; rows++)
{
if (numbersGrid[rows][0] == '1' && longestLineLength == 1)
{
numberOfLongestLines++;
}
for (int cols = 1; cols < N; cols++)
{
if (numbersGrid[rows][cols] == '1' && numbersGrid[rows][cols] == numbersGrid[rows][cols-1])
{
currentLineLength++;
if (currentLineLength > longestLineLength)
{
longestLineLength = currentLineLength;
numberOfLongestLines = 1;
}
else if(currentLineLength == longestLineLength)
{
numberOfLongestLines++;
}
}
else if (numbersGrid[rows][cols] == '1' && longestLineLength == 1)
{
numberOfLongestLines++;
}
else
{
currentLineLength = 1;
}
}
currentLineLength = 1;
}

currentLineLength = 1;

//Check columns
for (int cols = 0; cols < N; cols++)
{
if (numbersGrid[0][cols] == '1' && longestLineLength == 1)
{
numberOfLongestLines++;
}
for (int rows = 1; rows < N; rows++)
{
if (numbersGrid[rows][cols] == '1' && numbersGrid[rows][cols] == numbersGrid[rows - 1][cols])
{
currentLineLength++;
if (currentLineLength > longestLineLength)
{
longestLineLength = currentLineLength;
numberOfLongestLines = 1;
}
else if (currentLineLength == longestLineLength)
{
numberOfLongestLines++;
}
}
else if (numbersGrid[rows][cols] == '1' && longestLineLength == 1)
{
numberOfLongestLines++;
}
else
{
currentLineLength = 1;
}
}
currentLineLength = 1;
}
if (longestLineLength==1)
{
numberOfLongestLines = numberOfLongestLines - 1;
numberOfLongestLines = numberOfLongestLines / 2;
}
Console.WriteLine(longestLineLength);
Console.WriteLine(numberOfLongestLines);
}
}
}

Условия и решения на задачите от тест „Telerik Software Academy – C# Fundamentals Part 1 – Sample Exam“

Задача 1. Problem-Cartesian Coordinate System

Условие:
You are given a two-dimensional Cartesian coordinate system and the two coordinates (X and Y) of a point in the coordinate system. If you don’t know what Cartesian coordinate system is Google it with Bing. As you will find, the coordinate system is divided by 2 lines (see the picture bellow) which divide the plain in four parts. Each of these parts has a lot of points that are numbered between 1 and 4. There is one point where our lines are crossing. This point has the following coordinates: X=0 and Y=0. As a result this point is numbered 0. The points on the lines are also numbered with the numbers 5 and 6 (again see the picture below).
Your task is to write a program that finds the number of the location of the given point in the coordinate system.
Practical exam_CSharp_Samplke exam_a

Input
Input data is being read from the console.
The number X is on the first input line.
The number Y is on the second input line.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output data must be printed on the console.
On the only output line you must print an integer number between 0 and 6, depending on the location of the given point in the coordinate system.

Constraints
• The numbers X and Y are numbers between -2 000 000 000 001 337 and 2 000 000 000 001 337, inclusive.
• Allowed working time for your program: 0.25 seconds.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Sample exam_b

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _1.Problem_CartesianCoordinateSystem
{
class CartesianCoordinateSystem
{
static void Main()
{
decimal X, Y;
decimal minInput = -2000000000001337;
decimal maxInput = 2000000000001337;
bool isXLong = decimal.TryParse(Console.ReadLine(), out X);
bool isYLong = decimal.TryParse(Console.ReadLine(), out Y);
if (isXLong && isYLong && X >= minInput && X <= maxInput && Y >= minInput && Y <= maxInput)
{
if (X == 0 && Y == 0)
{
Console.WriteLine("0");
}
else if (X == 0)
{
Console.WriteLine("5");
}
else if (Y == 0)
{
Console.WriteLine("6");
}
else if (Y > 0 && X > 0)
{
Console.WriteLine("1");
}
else if (Y > 0 && X < 0)
{
Console.WriteLine("2");
}
else if (Y < 0 && X < 0)
{
Console.WriteLine("3");
}
else if (Y < 0 && X > 0)
{
Console.WriteLine("4");
}
}
else
{
Console.WriteLine("Wrong entry!");
}
}
}
}

Задача 2. Problem-Miss Cat 2011

Условие:
There are two things that cats love most: 1) sleeping and 2) attending beauty contests. The most important thing for each female cat is the contest “Miss Cat”. There are always ten cats that participate in the final round of the contest, numbered 1 to 10.
The jury of the contest consists of N people who subjectively decide which cat to vote for. In other words each person votes for just 1 cat that he has most liked, or from whose owner he has received the biggest bribe. The winner of the contest is the cat that has gathered most votes. If two cats have equal votes, the winner of the contest is the one whose number is smaller.
Your task is to write a computer program that finds the number of the cat that is going to win the contest “Miss cat”

Input
The input data is being read from the console.
The number N is on the first input line.
An integer between 1 and 10 is written on each of the next N lines (this is the number of the cat)
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output data must be printed on the console.
On the only output line you must print the number of the cat, which has won the contest.

Constraints
• The number N is a positive integer between 1 and 100 000, inclusive.
• The numbers of the cats for which the jury votes are positive integer numbers between 1 and 10, inclusive.
• Allowed working time for your program: 0.25 second.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Sample exam_c

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _2.Problem_MissCat2011
{
class MissCat2011
{
static void Main()
{
int N = int.Parse(Console.ReadLine());
int[] votes = new int[N];
int voteCounter = 1;
int currentWinner = 0;
int winnerScore = 0;
if (N > 0 && N < 100001)
{
for (int i = 0; i < N; i++)
{
votes[i] = int.Parse(Console.ReadLine());
}
Array.Sort(votes);
currentWinner = votes[0];
for (int i = 1; i < N; i++)
{
if (votes[i] == votes[i - 1])
{
voteCounter++;
if (winnerScore < voteCounter)
{
currentWinner = votes[i];
winnerScore = voteCounter;
}
}
else
{
voteCounter = 1;
}
}
Console.WriteLine(currentWinner);
}
else
{
Console.WriteLine("Wrong entry!");
}
}
}
}

Задача 3. Problem-Forest Road

Условие:
Geeko, a non-stop learning trainee at Telerik Software Academy lived deep into the Lulin forests. Every time he went to the Academy he had to take a long trip through the forest. Starting from the top left corner of the forest, the road always goes down and right first and when it reaches the border, it goes down and left.
The Academy is situated in the bottom left corner, and Geeko begins his journey from the top left corner of the forest (see the examples below).
He wanted to make a program that generates a map of the forest but he couldn’t. Help Geeko on his way to the Academy by writing the program instead of him.

Input
The input data is being read from the console.
On the only line in the console you are given an integer number N, showing the width of the map. The map’s height is always 2*N – 1.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output data must be printed on the console.
You should print the whole map on the console. Use the symbol “*” (asterisk) to mark Geeko’s path and “.” (dot) to illustrate the trees.

Constraints
• The number N is a positive integer between 2 and 79, inclusive.
• Allowed working time for your program: 0.25 second.
• Allowed memory: 16 MB.

Samples
Practical exam_CSharp_Sample exam_d

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _3.Problem_ForestRoad
{
class ForestRoad
{
static void Main()
{
int N = int.Parse(Console.ReadLine());
if (N > 1 && N < 80)
{
char[] dotArray = new char[N];
for (int i = 0; i < N; i++)
{
dotArray[i] = '.';
}
for (int i = 0; i < N; i++)
{
dotArray[i] = '*';
for (int j = 0; j < N; j++)
{
Console.Write(dotArray[j]);
}
dotArray[i] = '.';
Console.WriteLine();
}
for (int i = N - 2; i >= 0; i--)
{
dotArray[i] = '*';
for (int j = 0; j < N; j++)
{
Console.Write(dotArray[j]);
}
dotArray[i] = '.';
Console.WriteLine();
}
}
else
{
Console.WriteLine("Wrong entry!");
}
}
}
}

Задача 4. Problem-Binary Digits Count

Условие:
You are given a sequence of N positive integer numbers and one binary digit B (0 or 1).
Your task is to write a program that finds the number of binary digits (B) in each of the N numbers in binary numeral system. Example: 20 in the binary numeral system looks like this: 10100. The number of binary digits 0 of the number 20 in the binary numeral system is 3.

Input
The input data is being read from the console.
On the first input line there will be the digit B.
On the second line you must read the number N.
On each of the following N lines there is one positive integer number written – the consequent number, whose sum of binary digits B we are searching for.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output must be printed on the console.
In the output you must have N lines. Each line must have 1 integer number – the number of digits B in the binary representation of the given consequent number.

Constraints
• Number N is a positive integer between 1 and 1000, inclusive.
• Each of the N numbers is a positive integer between 1 and 4 000 000 000, inclusive.
• The digit B will be only 0 or 1.
• Allowed work time for your program: 0.25 second.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Sample exam_e

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _4.Problem_BinaryDigitsCount
{
class BinaryDigitsCount
{
public static uint NumberOfBits(uint i)
{
uint numberOfBits = 0;
while (true)
{
i = i / 2;
numberOfBits++;
if (i == 0)
{
break;
}
}
return numberOfBits;
}
static void Main()
{
uint mask = 0;
bool IsNCorrect = true;
uint B = uint.Parse(Console.ReadLine());
uint N = uint.Parse(Console.ReadLine());
uint[] numbers = new uint[N];
uint[] numberOfB = new uint[N];
for (uint i = 0; i < N; i++)
{
numbers[i] = uint.Parse(Console.ReadLine());
if (numbers[i] < 1 || numbers[i] > 4000000000)
{
IsNCorrect = false;
}
}
if (N > 0 && N < 1001 && IsNCorrect && (B == 0 || B == 1))
{
for (uint i = 0; i < N; i++)
{
uint numberOfbits = NumberOfBits(numbers[i]);
for (uint bit = 0; bit < numberOfbits; bit++)
{
if (B == 1)
{
mask = B << (int)bit;
if ((numbers[i] & mask) == (B << (int)bit))
{
numberOfB[i]++;
}
}
else //B==0
{
mask = (uint)1 << (int)bit;
mask = ~mask;
if ((numbers[i] | mask) == mask)
{
numberOfB[i]++;
}
}

}
}
for (int i = 0; i < N; i++)
{
Console.WriteLine(numberOfB[i]);
}
}
else
{
Console.WriteLine("Wrong entry!");
}
}
}
}

Задача 5. Problem-Subset Sums

Условие:
You are given a list of N numbers. Write a program that counts all non-empty subsets from this list, which have sum of their elements exactly S.
Example: if you have a list with 4 elements: { 1, 2, 3, 4 } and you are searching the number of non-empty subsets which sum is 4, the answer will be 2. The subsets are: { 1, 3 } and { 4 }.

Input
The input data is being read from the console.
On the first input line there will be the number S.
On the second line you must read the number N.
On each of the following N lines there will be one integer number written – all the numbers from the list.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output must be printed on the console.
On the only output line you must print the number of the non-empty subsets, which have sum of all its elements exactly S.

Constraints
• The number N is a positive integer between 1 and 16, inclusive.
• All of the N numbers are integer numbers and will be between -1 337 000 000 000 and 1 337 000 000 000, inclusive.
• The number S is an integer number between -21 392 000 000 000 and 21 392 000 000 000, inclusive.
• All of the N numbers will be distinct.
• Allowed work time for your program: 1 second.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Sample exam_f

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _5.Problem_SubsetSums
{
class SubsetSums
{
static void Main()
{
bool isLong = true;
byte elementsEqualToS = 0;
long S = long.Parse(Console.ReadLine());
int N = int.Parse(Console.ReadLine());
long[] longNumbers = new long[N];
if (N > 0 && N < 17)
{
for (int i = 0; i < N; i++)
{
isLong = long.TryParse(Console.ReadLine(), out longNumbers[i]);
if (isLong == false)
{
break;
}
if (longNumbers[i] == S)
{
elementsEqualToS++;
}
}
if (isLong)
{
Console.WriteLine(FindSubsetSum(longNumbers, S) + elementsEqualToS);
}
else
{
Console.WriteLine("Wrong entry! Some of the numbers are not long!");
}
}
else
{
Console.WriteLine("Wrong entry! The members of the sequence are not between 1 and 16!");
}
}
private static int FindSubsetSum(long[] numbers, long S)
{
int numberOfSubsets = 0;
int allSubsets = (int)Math.Pow(2, numbers.Length);
long[] dynamicSums = new long[allSubsets];
for (int i = 1; i < numbers.Length; i++)// numbers in the array
{
int j, k;
for (j = 0; j < ((1 << i) - (i + 1)); j++)// the sum with already summed elements
{

if ((dynamicSums[(1 << i) - (i + 1) + j] = numbers[i] + dynamicSums[j]) == S)
{
numberOfSubsets++;
}
}
for (k = 0; k < i; k++)// the sums with the new elements
{
if ((dynamicSums[(1 << i) - (i + 1) + (j + k)] = numbers[k] + numbers[i]) == S)
{
numberOfSubsets++;
}
}
}
return numberOfSubsets;
}
}
}

Условия и решения на задачите от тест „Telerik Software Academy 2011 / 2012 – C# Fundamentals Part 1 – Test Exam“

Задача 1. Problem-Math Expression

Условие:
You are given the following mathematical expression:

The sin(x) is a trigonometric function that returns the sine from the angle x (measured in radians).
The mod operator finds the remainder of division of one number by another.
Here are some examples for how the mod operator should work:
• 5 mod 2 = 1
• 5.99 mod 3 = 2
• 6 mod 3 = 0
Your task is to write a computer program that calculates the result from the shown mathematical expression, depending on the values of the variables N, M and P.

Input
The input data is being read from the console.
The input consists of exactly 3 lines. In each line you consequently enter the variables N, M and P.
The separator between the integer and the fractional part of the number is “.” (dot).
The number of digits that follow the decimal point will not be more than 6.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output data must be printed on the console.
There must be only one line, showing the result from the mathematical expression.
The result must show exactly 6 digits after the “.” (decimal point).

Constraints
• The numbers N, M and P are fractional numbers.
• N, M and P will be between -10 000 000 and 10 000 000, inclusive.
• The numbers M and P will always have values other than 0
• It is guaranteed that none of the combinations of the numbers N, M and P will lead to dividing by zero.
• Allowed working time for your program: 0.10 seconds.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Practicle exam_a

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _1.Problem_MathExpression
{
class MathExpression
{
static void Main()
{
decimal N, M, P;
decimal minInput = -10000000;
decimal maxInput = 10000000;
decimal result;

N = decimal.Parse(Console.ReadLine());
M = decimal.Parse(Console.ReadLine());
P = decimal.Parse(Console.ReadLine());
if (N >= minInput && N <= maxInput && M >= minInput && M != 0 && M <= maxInput && P >= minInput && P != 0 && P <= maxInput)
{

result = (((N * N) + (1 / (M * P)) + 1337) / (N - (128.523123123M * P))) + (decimal)Math.Sin((double)((int)(M % 180)));
Console.WriteLine("{0:0.000000}", result);
}
else
{
Console.WriteLine("Wrong entry!");
}
}
}
}

Задача 2. Problem-Least Majority Multiple

Условие:
Given five positive integers, their least majority multiple is the smallest positive integer that is divisible by at least three of them.
Your task is to write a program that for given distinct integers a, b, c, d and e, returns their least majority multiple.
For example if we have 1, 2, 3, 4 and 5 the majority multiple of the given five numbers is 4 because it is divisible by 1, 2, and 4.
Another example: if we have 30, 42, 70, 35 and 90 the answer will be 210, because it is divisible by 30, 42, 70, and 35 – four out of five numbers, which is a majority.

Input
The input data is being read from the console.
The input data will consist of 5 lines.
The numbers a, b, c, d and e will each be on a single line.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output data must be printed on the console.
On the only output line you must print the least majority multiple of the given numbers.

Constraints
• a, b, c, d and e will each be integer numbers between 1 and 100, inclusive.
• a, b, c, d and e will be distinct.
• Allowed working time for your program: 0.25 seconds.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Practicle exam_b

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _2.Problem_LeastMajorityMultiple
{
class LeastMajorityMultiple
{
static void Main()
{
int[] numbers = new int[5];
bool isValidEntry = true;
byte numberDivisable = 0;
for (int i = 0; i < 5; i++)
{
numbers[i] = int.Parse(Console.ReadLine());
if (numbers[i] <= 0 || numbers[i] >= 101)
{
isValidEntry = false;
}
}
if (isValidEntry)
{
Array.Sort(numbers);
for (long i = numbers[2]; i <= numbers[2] * numbers[3] * numbers[4]; i++)
{
for (int p = 0; p < 5; p++)
{
if (i % numbers[p] == 0)
{
numberDivisable++;
}
}
if (numberDivisable > 2)
{
Console.WriteLine(i);
break;
}
else
{
numberDivisable = 0;
}
}
}
else
{
Console.WriteLine("Wrong entry!");
}
}
}
}

Задача 3. Problem-Trapetzoid

Условие:
Write a program that prints on the console the border of a trapezoid by given number N.
The width of the top side of the trapezoid must be exactly N.
The width of the bottom side of the trapezoid must be exactly 2 * N.
The height of the trapezoid must be exactly N + 1.
Also the top right and the bottom right angle of the trapezoid must be equal to 90 degrees.
See the examples bellow.

Input
The input data is being read from the console.
On the only line in the console you are given an integer number N, showing the width of the smallest trapezoid side.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output data must be printed on the console.
You must write the border of the described trapezoid on the console.
Use the symbol “*” (asterisk) to mark the border of the trapezoid.
Use the symbol “.” (dot) to illustrate the empty spaces outside and inside the trapezoid.

Constraints
• The number N is a positive integer between 3 and 39, inclusive.
• Allowed working time for your program: 0.25 seconds.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Practicle exam_c

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _3.Problem_Trapetzoid
{
class Trapetzoid
{
static void Main()
{
int N = int.Parse(Console.ReadLine());
char[] line = new char[2 * N];
for (int i = 0; i < 2 * N; i++)
{
line[i] = '.';
}
for (int i = 0; i < N + 1; i++)
{
if (i == 0)
{
for (int p = N; p < 2 * N; p++)
{
line[p] = '*';
}
}
else if (i == 1)
{
for (int p = N; p < (2 * N) - 1; p++)
{
line[p] = '.';
}
line[N - i] = '*';
}
else if (i == N)
{
for (int p = 0; p < 2 * N; p++)
{
line[p] = '*';
}
}
else
{
line[N - i] = '*';
line[N - i + 1] = '.';
line[(2 * N) - 1] = '*';
}
for (int p = 0; p < 2 * N; p++)
{
Console.Write(line[p]);
}
Console.WriteLine();
}
}
}
}

Задача 4. Problem-Odd Number

Условие:
You are given a list of N integer numbers all but one of which appears an even number of times.
Write a program to find the one integer which appears an odd number of times.

Input
The input data is being read from the console.
The number N is written on the first input line.
On each of the following N lines there is one integer number written – the consequent number from the given list of numbers.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output data must be printed on the console.
On the only output line you must print the integer from the list which appears an odd number of times.

Constraints
• N will be positive odd integer number between 1 and 99 999, inclusive.
• All of the numbers in the list will be integer numbers between -9 223 372 036 854 775 808
and 9 223 372 036 854 775 807, inclusive.
• Always only one answer will exists and will be unambiguous.
• Allowed working time for your program: 0.25 seconds.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Practicle exam_d

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _4.Problem_OddNumber
{
class OddNumber
{
static void Main()
{
int N = int.Parse(Console.ReadLine());
long currentNumber = 0;
int oddNumbersCounter = 1;
if (N % 2 != 0)
{
long[] numbers = new long[N];
for (int i = 0; i < N; i++)
{
numbers[i] = long.Parse(Console.ReadLine());
}
Array.Sort(numbers);
for (int i = 0; i < N; i++)
{
if (i == 0)
{
currentNumber = numbers[i];
}
else
{
if (currentNumber == numbers[i])
{
oddNumbersCounter++;
}
else
{
if (oddNumbersCounter % 2 == 0)
{
currentNumber = numbers[i];
oddNumbersCounter = 1;
}
else
{
Console.WriteLine(numbers[i - 1]);
return;
}
}
}
}
Console.WriteLine(numbers[N - 1]);
}
else
{
Console.WriteLine("Wrong entry!");
}
}
}
}

Задача 5. Problem-Fall Down

Условие:
You are given a list of 8 bytes (positive integers in the range [0…255]) n0, n1, …, n7. These numbers represent a square grid consisting of 8 lines and 8 columns. Each cell of the grid could either be empty or full. The first line is represented by the bits of n0, the second – by the bits of n1 and so on, and the last line is represented by the bits of n7. Each bit with value 1 denotes a full cell and each bit with value 0 denotes an empty cell. The lines are numbered from the first (top) to the last (bottom) with the numbers 0, 1, …, 7. The columns are numbered from right to left with the indices 0, 1, …, 7. The figure shows a sample square grid and its representation by a sequence of 8 numbers n0, n1, …, n7:
Practical exam_CSharp_Practicle exam_e

Suppose the full cells hold squares which can „fall down“ by the influence of the gravity. Each full cell in certain row and column falls down to the lowest row possible but stays in the same column and up from any other full cells on the same column that ware initially down from it. At the figure the „fall down“ process is illustrated.
Write a program to calculate how the grid will look like after the „fall down“ process is applied.

Input
The input data is being read from the console.
There will be exactly 8 lines each holding the integer numbers n0, n1, …, n7.
The input data will always be valid and in the format described. There is no need to check it explicitly.

Output
The output consists of the numbers n0, n1, …, n7 after the „fall down process“.
Ouput should be printed on the console, in exactly 8 lines, each holding a single integer.

Constraints
• The numbers n0, n1, …, n7 are positive integers between 0 and 255, inclusive.
• Allowed work time for your program: 0.25 seconds.
• Allowed memory: 16 MB.

Examples
Practical exam_CSharp_Practicle exam_f

Решение:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _5.Problem_FallDown
{
class FallDown
{
static void Main()
{
byte[] bytes = new byte[8];
byte[] numberOfBitsOnPosition = new byte[8];
byte[] newBytes = new byte[8];
for (int i = 0; i < 8; i++)
{
bytes[i] = byte.Parse(Console.ReadLine());
}
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if ((bytes[i] & (1 << j)) != 0)
{
numberOfBitsOnPosition[j]++;
}
}
}
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (numberOfBitsOnPosition[j] > 0)
{
newBytes[i] = (byte)(newBytes[i] | 1 << j);
numberOfBitsOnPosition[j]--;
}
}
}
for (int i = 7; i >= 0; i--)
{
Console.WriteLine(newBytes[i]);
}

}
}
}