DEV Community

Sunnat Qayumov
Sunnat Qayumov

Posted on

HackerRank - Problem#12

Kodning vazifasi:

Kodni ishlatib, ma'lum bir oraliq uylar ichiga tushadigan olma va apelsinlarning sonini hisoblaymiz. Olmalar va apelsinlar turli xil manzildan tushadi va har biri alohida masofada joylashgan.

Toliq kod:

class Solution
{
    static void Main()
    {
        string[] st = Console.ReadLine().Split(' ');

        string s = st[0];
        string t = st[1];

        string[] ab = Console.ReadLine().Split(' ');

        string a = ab[0];
        string b = ab[1];

        string[] mn = Console.ReadLine().Split(' ');

        string m = mn[0];
        string n = mn[1];

        string[] apples = Console.ReadLine().Split(' ');
        string[] oranges = Console.ReadLine().Split(' ');

        int applesCount = 0;
        int orangesCount = 0;

        foreach(string apple in apples)
        {
            int result = int.Parse(a) + int.Parse(apple);

            if(result >= int.Parse(s) && result <= int.Parse(t))
            {
                applesCount++;
            }
        }
        foreach(string orange in oranges)
        {
            int result = int.Parse(b) + int.Parse(orange);

            if(result >= int.Parse(s) && result <= int.Parse(t))
            {
                orangesCount++;
            }
        }
        Console.WriteLine(applesCount);
        Console.WriteLine(orangesCount);
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Kodni qadamma - qadam ko'rib chiqamiz.

1. Input qilish

string[] st = Console.ReadLine().Split(' ');
string s = st[0];
string t = st[1];
Enter fullscreen mode Exit fullscreen mode
  • s va t -> elementlari uyning boshlanishi va tugashini anglatadi.
 string[] ab = Console.ReadLine().Split(' ');
 string a = ab[0];
 string b = ab[1];
Enter fullscreen mode Exit fullscreen mode
  • a va b -> olma daraxti va apelsin daraxti joylashuvini anglatadi.
string[] mn = Console.ReadLine().Split(' ');
string m = mn[0];
string n = mn[1];
Enter fullscreen mode Exit fullscreen mode
  • m va n -> olma va apelsinlar necha donaligini anglatadi.
string[] apples = Console.ReadLine().Split(' ');
string[] oranges = Console.ReadLine().Split(' ');
Enter fullscreen mode Exit fullscreen mode
  • apples va oranges -> har bir olma va apelsinning uyga tushgan masofasini korsatadi.

2. Natijalarni hisoblash

int applesCount = 0;
int orangesCount = 0;
Enter fullscreen mode Exit fullscreen mode
  • applesCount va orangesCount -> uy radiusiga tushgan olmalar va apelsinlar sonini saqlaydi.

3. Olmalarni hisoblash:

foreach(string apple in apples)
{
    int result = int.Parse(a) + int.Parse(apple);

    if(result >= int.Parse(s) && result <= int.Parse(t))
    {
        applesCount++;
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Har bir olma daraxti tushgani a va unga tushish masofasi apple qo'shish orqali hisoblanadi. Agar uy koordinatasi s dan t gacha bo'lgan masofaga olmalar tushgan bo'lsa applesCount qiymati bittaga oshiriladi.

4. Aplesinlarni hisoblash:

foreach(string orange in oranges)
{
    int result = int.Parse(b) + int.Parse(orange);

    if(result >= int.Parse(s) && result <= int.Parse(t))
    {
        orangesCount++;
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Har bir apelsin daraxti tushgani b va unga tushish masofasi orange qo'shish orqali hisoblanadi. Agar uy koordinatasi s dan t gacha bo'lgan masofaga apelsinlar tushgan bo'lsa orangesCount qiymati bittaga oshiriladi.

5. Natijani chop etish:

Console.WriteLine(applesCount);
Console.WriteLine(orangesCount);
Enter fullscreen mode Exit fullscreen mode
  • Hisoblab bo'lingan olma va apelsinlar Consolega chop etiladi.

Top comments (0)