HackerRank Problem#13

sunnat_qayumov

Sunnat Qayumov

Posted on November 21, 2024

HackerRank Problem#13

Problem: Grading Students.

Bu masalada talabaning baholari yaxlitlanishi kerak bo'ladi. Har bir baho eng yaqin 5 ga yaxlitlanishi kerak, agar bu qiymat 3 yoki undan yuqori bo'lsa bahoni yangilanadi.

Kod:

class Program
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        int[] grades = new int[n];

        for (int i = 0; i < n; i++)
        {
            int grade = int.Parse(Console.ReadLine());
            if (grade >= 38)
            {
                int result = ((grade / 5) + 1) * 5;
                if (result - grade < 3)
                {
                    grade = result;
                }
            }
            grades[i] = grade;
        }
        for (int i = 0; i < n; i++)
        {
            Console.WriteLine(grades[i]);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Kodni qadamma-qadam ko'rib chiqamiz.

1. Kiritish:

int n = int.Parse(Console.ReadLine());
int[] grades = new int[n];
Enter fullscreen mode Exit fullscreen mode
  • int n = int.Parse(Console.ReadLine());: Talaba sonini (n) kiritamiz.
  • int[] grades = new int[n];: Talabalarning baholarini saqlash uchun array yaratamiz, bu array uzunligi talaba soniga teng bo‘ladi.

2. Baholarni o'qish va qaytarish:

for (int i = 0; i < n; i++)
{
    int grade = int.Parse(Console.ReadLine());
    if (grade >= 38)
    {
        int result = ((grade / 5) + 1) * 5;
        if (result - grade < 3)
        {
            grade = result;
        }
    }
    grades[i] = grade;
}
Enter fullscreen mode Exit fullscreen mode
  • int grade = int.Parse(Console.ReadLine());: Hozirgi talabaning bahosini o‘qib, grade o‘zgaruvchisiga saqlaymiz.
  • if (grade >= 38): Agar baho 38 yoki undan katta bo‘lsa, yaxlitlashni ko‘rib chiqamiz. Agar baho 38 dan kichik bo‘lsa, yangilash kerak emas.
  • int result = ((grade / 5) + 1) * 5;:
    • Bu ifoda yordamida bahodan keyingi eng yaqin 5 ko‘paytmasini topamiz. Misol: agar grade = 73 bo‘lsa: 73 / 5 = 14 (butun bo‘linish). (14 + 1) * 5 = 75 (keyingi 5 ko‘paytmasi).
  • if (result - grade < 3):

  • Agar keyingi 5 ko‘paytmasiga bo‘lgan farq 3 dan kichik bo‘lsa, bahoni yaxlitlanadi.

  • Misol, agar grade = 73 bo‘lsa:

    • 75 - 73 = 2 (farq 3 dan kichik).
    • Shunday qilib, grade 75 ga o‘zgartiriladi.
  • grades[i] = grade;: O‘zgartirilgan yoki o‘zgarmagan baho massivga saqlanadi.

3. Natijani chop etish:

for (int i = 0; i < n; i++)
{
    Console.WriteLine(grades[i]);
}
Enter fullscreen mode Exit fullscreen mode
  • for tsikli orqali barcha qayta ishlangan baholar massivdan chiqariladi.
  • Console.WriteLine(grades[i]);: Har bir talabaning bahosi yangi qatorda chop etiladi.
💖 💪 🙅 🚩
sunnat_qayumov
Sunnat Qayumov

Posted on November 21, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related