DEV Community

IRMuhammadjon
IRMuhammadjon

Posted on

👑Kichik Shahzoda

🚀 Kichik Shahzoda: C# da Sayyoralararo Sayohat Masalasi

Salom, dasturchilar! 👋 Bugun sizlar bilan "Kichik Shahzoda" nomli qiziqarli masalaning C# yechimini ko'rib chiqamiz. Bu masala sayyoralararo sayohat haqida bo'lib, uchta asosiy fayldan iborat: Program.cs, Point.cs va Planet.cs.

🧱 Asosiy Strukturalar

📍 Point Strukturasi

public struct Point(int x, int y)
{
    public int X { get; set; } = x;
    public int Y { get; set; } = y;
    // ... boshqa kodlar ...
}
Enter fullscreen mode Exit fullscreen mode

🌎 Planet Sinfi

public struct Planet(Point position, int radius)
{
    public Point Position { get; set; } = position;
    public int Radius { get; set; } = radius;
    // ... boshqa kodlar ...
}
Enter fullscreen mode Exit fullscreen mode

🧮 Asosiy Algoritm

int n = int.Parse(Console.ReadLine()!);

while(n-- > 0)
{
    var s = Console.ReadLine()!.Split().Select(int.Parse).ToArray();
    var prince = new Point(s[0], s[1]);
    var princess = new Point(s[2], s[3]);
    // ... boshqa kodlar ...
}
Enter fullscreen mode Exit fullscreen mode

🔍 Qiziqarli Qismlar

  1. Sayyora ichida ekanligini tekshirish 🌌:
public bool IsInside(Point point)
{
   var a = point.X - Position.X;
   var b = point.Y - Position.Y;
   return Math.Sqrt(a * a + b * b) <= Radius;
}
Enter fullscreen mode Exit fullscreen mode
  1. Kiritishni qayta ishlash ⌨️:
public Planet(string? s) : this(new Point(), 0)
{
    if(s == null || s.Split(' ',StringSplitOptions.RemoveEmptyEntries).Length != 3)
        throw new ArgumentException("Invalid input format");
    // ... boshqa kodlar ...
}
Enter fullscreen mode Exit fullscreen mode
  1. Yo'l hisoblash 🧭:
if(planet.IsInside(prince) ^ planet.IsInside(princess))
    count++;
Enter fullscreen mode Exit fullscreen mode

🎉 Xulosa

Bu yechim sayyoralararo sayohat masalasini hal qilishning samarali usulini ko'rsatadi.Quyidagi GitHub repostoriyamda dastur kodi joylangan. To'liq kod:

🔗 GitHub Repository: Masalaning yechimi

Agar savollaringiz yoki takliflaringiz bo'lsa, izohlar bo'limida qoldiring! 💬

csharp #algorithms #programming

Top comments (0)