URI problem 1061 - C#

Asked

Viewed 546 times

0

I have a problem that the code below it results me the correct result, however, when I send to the site it informs the error RUNTIME (vector or array with less capacity than required for the problem, or when you try to access an invalid memory. ) I can’t see the reason for the mistake, someone could help me??

Exercise website for viewing: https://www.urionlinejudge.com.br/judge/pt/problems/view/1061

            Console.Write("Dia ");
        int diaI = int.Parse(Console.ReadLine());

        // vetor string dados ínicio da festa
        string line = Console.ReadLine();
        string[] tempoI = line.Split(':');
        int horaI = int.Parse(tempoI[0]);
        int minI = int.Parse(tempoI[1]);
        int segI = int.Parse(tempoI[2]);

        Console.Write("Dia ");
        int diaF = int.Parse(Console.ReadLine());

        // vetor string dados encerramento da festa
        string line2 = Console.ReadLine();
        string[] tempoF = line2.Split(':');
        int horaF = int.Parse(tempoF[0]);
        int minF = int.Parse(tempoF[1]);
        int segF = int.Parse(tempoF[2]);

        int diaT = 0, horaT = 0, minT = 0, segT = 0;

        //lógica para verificar tempo
        {
            //segundos
            if (segI > segF)
                segT = (60 - segI) + segF;
            else if (segI == segF)
                segT = 0;
            else
                segT = segF - segI;

            //minutos
            if (minI > minF)
                minT = (60 - minI) + minF;
            else if (minI == minF)
                minT = 0;
            else
                minT = minF - minI;

            //hora
            if (horaI > horaF)
            {
                horaT = (24 - horaI) + horaF;
                diaT = (diaF - diaI) - 1;
            }
            else if (horaI == horaF)
            {
                horaT = 0;
                diaT = (diaF - diaI) + 1;
            }
            else
            {
                horaT = horaF - horaI;
                diaT = (diaF - diaI);
            }

        }

        //imprimindo
        Console.WriteLine("{0} dia(s)", diaT);
        Console.WriteLine("{0} hora(s)", horaT);
        Console.WriteLine("{0} minuto(s)", minT);
        Console.WriteLine("{0} segundo(s)", segT);
  • It may be a matter of Culture, as the default date format on the server is different, such as MM/dd/YYYY

  • Opa leandro, as so matter of culture, because as the site debugs the code I think this would not interfere or not??

  • Pera, what you mean by send to the site, what you have is a console application

1 answer

0


The URI will not always send valid entries to your program, as I recall, and so your code should wait for errors and treat them.

Example: // Caso 1 int horaI = int.Parse(tempoI[0]); int minI = int.Parse(tempoI[1]); int segI = int.Parse(tempoI[2]); // Caso 2 int horaF = int.Parse(tempoF[0]); int minF = int.Parse(tempoF[1]); int segF = int.Parse(tempoF[2]);

If there is any invalid line reading, it would result in an Exception index out of Bounds due to not having an array of the size you are accessing. See if that’s not the problem.

I recommend using Tryparse instead of Parse. Another recommendation is you take a look at the Datetime class, I don’t know if it is possible/allowed to use it but it does all the calculation you do at hand easily. What little I used URI to do using C.

  • Thank you very much Kevin, but I had found the problem that will be in the first line, that the error was in the first line due to Console.Write, it always enters with the information and it is not necessary to print by the programmer, but now that I know Datatime and Tryparse, It would be much easier, but thank you!!! Ps: How I end this Question??

  • You can answer your question and mark it as an answer, right next to your answer will have a correct signal, just click and it will turn green. In case someone answers you correctly (in a future post), just mark this person as correct. I believe it is this way

Browser other questions tagged

You are not signed in. Login or sign up in order to post.