CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2011
    Posts
    5

    wrong answer on spoj.pl???

    why spoj.pl judge gives me wrong answer????

    problem #24 Small factorials

    here is the code:

    using System;

    class Program
    {
    static void Main()
    {
    long t = 0, n = 0, d = 0;
    long c = 0;
    t = Convert.ToInt64(Console.ReadLine().Trim());
    while (c < t)
    {
    n = Convert.ToInt64(Console.ReadLine().Trim());
    if (n < 100) ;
    d= factorial(n);
    Console.WriteLine(d);
    c++;
    }
    }

    static long factorial(long d)
    {

    long c = 0;
    long e = 1;
    long j = 1;
    while (c<d)
    {
    j = j * e;
    c++;
    e++;
    }
    return j;
    }
    }

    the code runs ok on visual basic, but spoj online judge gives me wrong answer... helppp.

  2. #2
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: wrong answer on spoj.pl???

    the following code appears to run OK on my machine (for the unconscionably few test points I tried) ....

    Code:
            // ****************************************
    
            private long myVersionOfHisMain() {
                return factorial(Int32.Parse(textBox2.Text));
            }
    
            private long factorial(long d)  {
    
                long c = 0;
                long e = 1;
                long j = 1;
                while (c < d)
                {
                    j = j * e;
                    c++;
                    e++;
                }
                return j;
            }
        // ********************************************************
    
    
       and called as follows ...
    
                textBox1.Text = myVersionOfHisMain().ToString();
    It is not entirely clear to me what your intent was in "Main", and it's too late at night for me to check the code ... the Tonight Show is almost on. I note what appears at first glance to be a "console.ReadLine" in the middle of a WHILE loop, that must be fun, and an IF that seems questionable to this admittedly novice programmer ...
    "if (n < 100) ;"

    But your "factorial" function came up running (at least insofar as my HP calculator was able to confirm).


    I had two versions of factorial ...
    Code:
            // ***************************************
            private long calculate(int n) {
    
                if (n >= 0)
                    if (n < 2)
                        return 1;
                    else
                        return n * calculate(n - 1);
                else
                    return -1;
            }
            // **************************************
    
    
            // **************************************
            private long AltCalculate(int n) {
                int cnt = 2;
                long product = 1;
    
                if (n >= 0)
                    while (cnt <= n) {
                        product *= cnt++;
                    }
                else
                    product = -1;
    
                return product;
            }
            // ***************************************
    
    
    called as follows ...
                textBox1.Text = calculate(Int32.Parse(textBox2.Text)).ToString();
    Last edited by ThermoSight; March 26th, 2011 at 01:38 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured