Професионална програма
Loading...
+ Нов въпрос
PikiMarinova avatar PikiMarinova 0 Точки

Suitcases Load Online Exam 28 and 19 March

Здрявейте,

Бихте ли ми помогнали за тази задача?

На втория тест ми дава Runtime error и не мога да си обясня защо.

Ето кода:

using System;
using System.Numerics;

namespace _05SuitcasesLoad
{
    class Program
    {
        static void Main(string[] args)
        {
            double planeCapacity = double.Parse(Console.ReadLine());
            string input = Console.ReadLine();
            int suitcaseCounter = 0;

            while (input != "End")
            {
                double suitcaseVolume = double.Parse(input);
                
                if (suitcaseCounter + 1 % 3 == 0)
                {
                    suitcaseVolume = suitcaseVolume * 1.1;
                }

                if (planeCapacity <= suitcaseVolume)
                {
                    break;
                }

                suitcaseCounter++;
                planeCapacity -= suitcaseVolume;
                input = Console.ReadLine();
                
                
            }

            if (input == "End")
            {
                Console.WriteLine("Congratulations! All suitcases are loaded!");
            }
            else
            {
                Console.WriteLine("No more space!");
            }

            Console.WriteLine($"Statistic: {suitcaseCounter} suitcases loaded.");

        }
    }
}
 

 

 

Благодаря!

Тагове:
0
Programming Basics with C#
Axiomatik avatar Axiomatik 100 Точки
Best Answer

This is one of those crazy exam-exercises where just the position of your Console.WriteLine can make or brake a test.

The only mistake I could find was that: if (suitcaseCounter + 1 % 3 == 0) needs to be included in additional brackets, like so : if ((suitcaseCounter + 1) % 3 == 0) . However, judge still doesn't give 100%, so I changed the position of the two Console.WriteLine to be positioned inside of the while-loop. CRAZY !!!

New code (100%):

using System;
using System.Numerics;

namespace _05SuitcasesLoad
{
    class Program
    {
        static void Main(string[] args)
        {
            double planeCapacity = double.Parse(Console.ReadLine());
            string input = Console.ReadLine();
            int suitcaseCounter = 0;

            while (planeCapacity >= 0)
            {
                if (input == "End")
                {
                    Console.WriteLine("Congratulations! All suitcases are loaded!");
                    break;
                }
                double suitcaseVolume = double.Parse(input);

                if ((suitcaseCounter + 1) % 3 == 0)
                {
                    suitcaseVolume = suitcaseVolume * 1.1;
                }

                if (planeCapacity <= suitcaseVolume)
                {
                    Console.WriteLine("No more space!");
                    break;
                }

                suitcaseCounter++;
                planeCapacity -= suitcaseVolume;
                input = Console.ReadLine();
            }

            Console.WriteLine($"Statistic: {suitcaseCounter} suitcases loaded.");
        }
    }
}

0
PikiMarinova avatar PikiMarinova 0 Точки

Thank you so much!

I would have never thought about that. 

0