1 / 4
Jan 2021

Ssup fellow coders
I just started my journey with spoj and I keep encoutering runtime error.
I tried to do the palindrome task. Here is my code (c#)
using System;
using System.Collections.Generic;

namespace Testowanie
{
    class Program
    {
        public static string Reverse(string s)
        {
            char[] charArray = s.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }
        public static bool checkStrings(string a, string b)
        {
            if (a.Length != b.Length)
                return false;
            for(int i = 0; i<a.Length/2; i++)
            {
                if (a[i] != b[i])
                    return false;
            }
            return true;
        }
        static void Main(string[] args)
        {
            int t = Convert.ToInt32(Console.ReadLine());
            List<double> lista = new List<double>();
            for (int i = 0; i < t; i++)
            {
                double nowy = Convert.ToDouble(Console.ReadLine());
                lista.Add(nowy);
            }
            foreach(var i in lista)
            {
                double zwiekszona = i + 1;
                string liczba = zwiekszona.ToString();
                string liczbaOdwrotna = Reverse(liczba);
                while (!checkStrings(liczba, liczbaOdwrotna))
                {
                    zwiekszona++;
                    liczba = zwiekszona.ToString();
                    liczbaOdwrotna = Reverse(liczba);
                }
                Console.WriteLine(liczba);                
            }
      
        }
    }
}

Whenever I try to submit this badboy I get a runtime error (NZEC). Sb please tell me why is this occuring?
Here is a link to the task : https://www.spoj.com/problems/PALIN/3

  • created

    Jan '21
  • last reply

    Jan '21
  • 3

    replies

  • 527

    views

  • 2

    users

  • 1

    like

  • 1

    link

Welcome to SPOJ.

Despite the large number of solvers, this problem isn’t as easy as it might first appear.

Did you notice the maximum size of the integer? I think test cases like this could be the cause of your problem.

1
999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999991

Thanks for your response, Simes!
The test case that you proposed indeed makes my program crash. However, should I use a try catch error statement if in the tasks description there is “For a given positive integer K of not more than 1000000 digits” ? Doesn’t that mean the SPOJ won’t test my program on numbers bigger than 10^6?

For a given positive integer K of not more than 1000000 digits

Read that bit again… 1000000 digits, 10^6 is only seven digits. The test case is valid.

Suggested Topics

Want to read more? Browse other topics in Online Judge System or view latest topics.