0% found this document useful (0 votes)
17 views9 pages

л2

This document contains the code and description for 4 programming tasks. The first task generates a random array of numbers between 0 and 100 and finds the nth largest element using quickselect. The second task generates points, finds the circle that contains the most points, and outputs the circle parameters. The third task sorts an array in descending order, finds the third largest element, and replaces smaller elements with this value. The fourth task contains the code and comments for the previous tasks.

Uploaded by

Fillin [YT]
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

л2

This document contains the code and description for 4 programming tasks. The first task generates a random array of numbers between 0 and 100 and finds the nth largest element using quickselect. The second task generates points, finds the circle that contains the most points, and outputs the circle parameters. The third task sorts an array in descending order, finds the third largest element, and replaces smaller elements with this value. The fourth task contains the code and comments for the previous tasks.

Uploaded by

Fillin [YT]
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Лабораторна робота №2

Виконав: студент 1 курсу


групи ФІТ 1-6
Шевченко Ігор
Задача 1
Задача 2
Задача 3
using System;

using System.Text;

class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;

int[] arr = new int[200];


Random rand = new Random();

for (int i = 0; i < arr.Length; i++)


{
arr[i] = rand.Next(101);
}

Console.WriteLine("Вхідний масив:");
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}

int n = 10;
int nthElement = QuickSelect(arr, n);

Console.WriteLine("\n\n{0}-й за величиною елемент: {1}\n", n, nthElement);

for (int i = 0; i < arr.Length; i++)


{
if (arr[i] < nthElement)
{
arr[i] = nthElement;
}
}

Console.WriteLine("Вихідний масив:");
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}

Console.ReadKey();
}

static int QuickSelect(int[] arr, int n)


{
int left = 0;
int right = arr.Length - 1;

while (true)
{
if (left == right)
{
return arr[left];
}

int pivotIndex = Partition(arr, left, right);


if (n == pivotIndex)
{
return arr[pivotIndex];
}
else if (n < pivotIndex)
{
right = pivotIndex - 1;
}
else
{
left = pivotIndex + 1;
}
}
}

static int Partition(int[] arr, int left, int right)


{
int pivotIndex = left + (right - left) / 2;
int pivotValue = arr[pivotIndex];

Swap(arr, pivotIndex, right);

int storeIndex = left;


for (int i = left; i < right; i++)
{
if (arr[i] < pivotValue)
{
Swap(arr, i, storeIndex);
storeIndex++;
}
}

Swap(arr, storeIndex, right);

return storeIndex;
}

static void Swap(int[] arr, int i, int j)


{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
Задача 4
//1
using System;
class Point
{
public double x, y;

public Point(double x, double y)


{
this.x = x;
this.y = y;
}
}

class Circle
{
public Point center;
public double radius;

public Circle(Point center, double radius)


{
this.center = center;
this.radius = radius;
}
}

class Program
{
static Point[] GeneratePoints(int n, int max)
{
Random rnd = new Random();
Point[] points = new Point[n];

for (int i = 0; i < n; i++)


{
double x = rnd.NextDouble() * max;
double y = rnd.NextDouble() * max;
points[i] = new Point(x, y);
}

return points;
}

static Circle CircleFromPoints(Point a, Point b, Point c)


{
double d = 2 * (a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y));
if (d == 0)
{
return null;
}

double x = ((a.x * a.x + a.y * a.y) * (b.y - c.y) + (b.x * b.x + b.y * b.y) * (c.y - a.y) + (c.x * c.x + c.y * c.y) * (a.y
- b.y)) / d;
double y = ((a.x * a.x + a.y * a.y) * (c.x - b.x) + (b.x * b.x + b.y * b.y) * (a.x - c.x) + (c.x * c.x + c.y * c.y) * (b.x
- a.x)) / d;

double radius = Math.Sqrt(Math.Pow(a.x - x, 2) + Math.Pow(a.y - y, 2));


return new Circle(new Point(x, y), radius);
}
static Circle FindCircleWithMostPoints(Point[] points)
{
Circle bestCircle = null;
int bestCount = 0;
for (int i = 0; i < points.Length - 2; i++)
{
for (int j = i + 1; j < points.Length - 1; j++)
{
for (int k = j + 1; k < points.Length; k++)
{
Circle circle = CircleFromPoints(points[i], points[j], points[k]);
if (circle == null)
{
continue;
}

int count = 0;
for (int m = 0; m < points.Length; m++)
{
if (Math.Sqrt(Math.Pow(points[m].x - circle.center.x, 2) + Math.Pow(points[m].y - circle.center.y, 2))
<= circle.radius + 0.00001)
{
count++;
}
}

if (count > bestCount)


{
bestCount = count;
bestCircle = circle;
}
}
}
}

return bestCircle;
}

static void Main(string[] args)


{
Point[] points = GeneratePoints(200, 100);

Console.WriteLine("Generated points:");
foreach (Point point in points)
{
Console.WriteLine("(" + point.x + ", " + point.y + ")");
}

Circle circle = FindCircleWithMostPoints(points);

Console.WriteLine("Circle with most points:");


Console.WriteLine("Center: (" + circle.center.x + ", " + circle.center.y + ")");
Console.WriteLine("Radius: " + circle.radius);
}
}
//Створюється масив точок з координатами (x, y), які генеруються випадковим чином в діапазоні від 0 до 100.
//Проходяться всі можливі комбінації трьох точок в масиві та для кожної комбінації обчислюється центр кола,
що проходить через ці три точки, а також його радіус.
//Зберігається кількість точок, які потрапляють в кожен з обчислених колів.
//Знаходиться той центр кола, радіус якого найбільший і в якому лежить найбільша кількість точок. Цей центр
і радіус є відповіддю на задачу.
//Виводяться згенеровані точки і знайдене коло з центром та радіусом.

//2
using System;
using System.Text;

class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
// створюємо масив з випадковими цілими числами
int[] arr = new int[10];
Random rnd = new Random();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = rnd.Next(0, 101);
}

// виводимо масив на екран


Console.WriteLine("Масив до заміни:");
Console.WriteLine(string.Join(", ", arr));

// сортуємо масив у порядку спадання


Array.Sort(arr);
Array.Reverse(arr);

// знаходимо третій за величиною елемент масиву


int thirdLargest = arr[2];

// замінюємо всі елементи масиву, які менші за третій за величиною елемент, на значення третього
елемента
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] < thirdLargest)
{
arr[i] = thirdLargest;
}
}

// виводимо масив на екран після заміни


Console.WriteLine("Масив після заміни:");
Console.WriteLine(string.Join(", ", arr));
}
}

You might also like