Kattis run time error solving Jumbo Javelin with C#

Viewed 271

I am new to Kattis and trying solve the challenge Jumbo Javelin with C# but I get run time error even though I am exiting my program with 0.

Here is my code:

using System;

namespace JumboJavelin
{
    class Program
    {
        static void Main(string[] args)
        {
            int l = int.Parse(Console.ReadLine());
            int sum = 0;
            int loss = 1;

            for (int n = 0; n <= 100; n++)
            {
                sum += l;

                if((n + 1) % 2 == 0 && !n.Equals(0))
                {
                    sum -= ((n + 1) / 2)*loss;
                }

                try
                {
                    l = int.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    break;
                }
            }
            Console.WriteLine(sum);
            Environment.Exit(0);
        }
    }
}

Can someone please help me? I know my solution eventually outputs the wrong answer but right now I am trying to get rid of the run time error since it works perfectly fine on Visual Studio 2019. All help is appreciated. Thanks

2 Answers

If you change the error your catch to Exception you will see that it is actually a wrong answer, so something goes wrong. You do exit your program with an exit code 0 at the bottom of your code, however, it throws an Exception somewhere else. Two things to consider:

  1. First, the problem statement explains that as a first input, you get an integer N (this N indicates the amount of steel rods he has) and CAN BE between 1 and 100 (1 not inclusive hence, 1<N<=100). You now have a loop that always loops from 1 to 100. And the loop should loop from 1 up to and including whatever value N is. Try to see what you can do about that.
  2. Second you read the input at the "tail" of the loop. This means that after the last cycle you do another read input. And that you do not read the first l. Try changing reading the input for l at the beginning of the loop. (and the first input you read in your Main, should not be for l (see point 1)).

See how far this gets you, is something is not clear please let me know.

As Kasper pointed out, n is dynamically provided on the first line; don't assume you have 100 lines to collect with for (int n = 0; n <= 100; n++).

There's no need to handle errors with the format; Kattis will always adhere to the format they specify. If something goes wrong parsing their input, there's no point trying to catch it; it's up to you to fix the misunderstanding. It's better to crash instead of catch so that you won't be fooled by Kattis telling you the failure was due to a wrong answer.

A simple approach is as follows:

  1. Collect n from the first line of input; this is the preamble
  2. Loop from 0 to n, collecting each line
  3. Accumulate the values per line into a sum
  4. Print sum - n + 1, the formula you can derive from their samples
using System;

class JumboJavelin
{
    static void Main(string[] args)
    {
        int n = int.Parse(Console.ReadLine());
        int sum = 0;

        for (int i = 0; i < n; i++)
        {
            sum += int.Parse(Console.ReadLine());
        }

        Console.WriteLine(sum - n + 1);
    }
}

You can use this pattern on other problems. You won't always aggregate one result after n lines of input though; sometimes the input is a single line or you have to print something per test case. Some problems require you to split and parse each line on a delimiter.

Regardless of the case, focus on gathering input cleanly as a separate step from solving the problem; only combine the two if you're confident you can do it without confusion or if your solution is exceeding the time limit and you're sure it's correct otherwise.

Related