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)