DEV Community

Muhammad Khodjaev
Muhammad Khodjaev

Posted on

Masala: Kichik shahzoda qutqaruvga otlandi

Masalaning sharti

Masalaning yechimi uchun uchta fayl lozim bo'ladi: Program.cs, Planet.cs va Point.cs.

Point.cs'da Struct ichida koordinatalar saqlash uchun X va Y property'lari mavjud:

public struct Point(int x, int y)
{
public int X { get; init; } = x;
public int Y { get; init; } = y;
}

Planet.cs'da bo'lsa Struct ichida Point va Radius property'lar bor va Pifagor formulasi yozilgan bo'lib u quyidagicha:

public bool IsInside(Point other)
    {
        var a = Center.X - other.X;
        var b = Center.Y - other.Y;
        var c = (a * a) + (b * b);

        var d = Math.Sqrt(c);

        return d <= Radius;
    }
Enter fullscreen mode Exit fullscreen mode

Ushbu kod qismi bo'lsa:

public Point Center { get; set; } = center;
    public int Radius { get; set; } = radius;

    public Planet(string input) : this(new(), 0)
    {
        var parts = input.Split(" ", StringSplitOptions.RemoveEmptyEntries)
                        .Select(int.Parse)
                        .ToArray() ?? [];

        Center = new Point(parts[0], parts[1]);
        Radius = parts[2];
    }
Enter fullscreen mode Exit fullscreen mode

planeta haqida ma'lumot saqlash uchun. Bu yerda shahzoda kesib o'tishi kerak bo'lgan planeta koordinatalari va radiusi saqlanadi.

Eng muhim qismi:

Eng muhim qismi bo'lsa Program.cs'da joylashgan bo'lib u yerda ma'lumotlarni foydalanuvchidan qabul qilib olamiz va IsInside() metodini ishlatib shahzoda planetani kesib o'tishi kerakmi yoki yo'qmi tekshiramiz. So'ngra har bir testcase'dan so'ng nechta planetani kesib o'tadi shuni console'ga chiqaramiz:

int testcases;
int.TryParse(Console.ReadLine() ?? "0", out testcases);


while (testcases-- > 0)
{
    var crossings = 0;

    var addresses = Console.ReadLine()?
                        .Split(" ", StringSplitOptions.RemoveEmptyEntries)
                        .Select(int.Parse).ToArray() ?? [];

    var princePoint = new Point(addresses[0], addresses[1]);
    var princessPoint = new Point(addresses[2], addresses[3]);

    var planetCount = int.Parse(Console.ReadLine() ?? "0");
    var planets = new Planet[planetCount];

    for (int i = 0; i < planets.Length; i++)
    {
        var planet = new Planet(Console.ReadLine() ?? "");  

        var isPrinceInside = planet.IsInside(princePoint);
        var isPrincessInside = planet.IsInside(princessPoint);
        var crosses = isPrinceInside ^ isPrincessInside;

        crossings += crosses ? 1 : 0;
    }

    Console.WriteLine(crossings);
}
Enter fullscreen mode Exit fullscreen mode

Agarda masala bo'yicha savollaringiz bo'lsa yoki xatolik ko'rsangiz Telegram orqali: @muhammad_khodjaev'ga yozing!

Top comments (0)