Steve M
Steve M

Reputation: 9784

Silly square root puzzle

I've been stuck on this problem for quite some time, it seems to work fine when I enter input in the console, but when submitted my solution always fails test #3 (you can not see what the input or output is). The problem is here Timus. Here is the problem:

The problem is so easy, that the authors were lazy to write a statement for it!

Input

The input stream contains a set of integer numbers Ai (0 ≤ Ai ≤ 1018). The numbers are separated by any number of spaces and line breaks. A size of the input stream does not exceed 256 KB.

Output

For each number Ai from the last one till the first one you should output its square root. Each square root should be printed in a separate line with at least four digits after decimal point.

Input:

1427  0   

     876652098643267843 

5276538

Output:

2297.0716

936297014.1164

0.0000

37.7757

This is my code:

using System;
using System.Collections.Generic;
using System.IO;

namespace ReverseRoot
{
class Program
{
    static void Main(string[] args)
    {
        List<ulong> longs = new List<ulong>();

        string current = "";
        bool inNumber = false;

        string input = Console.In.ReadToEnd();
        for (int i = 0; i < input.Length; ++i)
        {
            char c = input[i];
            if (inNumber && !char.IsDigit(c))
            {
                longs.Add(ulong.Parse(current));
                inNumber = false;
                current = "";
            }
            if (char.IsDigit(c))
            {
                inNumber = true;
                current += c;
            }
        }

        longs.Reverse(0, longs.Count);

        foreach (ulong n in longs)
        {
            double p = (Math.Truncate(Math.Sqrt(n) * 10000.0)) / 10000.0;
            Console.WriteLine("{0:F4}", p);
        }
        Console.ReadLine();
    }
}
}

I've also tried Rounding to four decimal places since the wording of the problem is not entirely clear:

        foreach (ulong n in longs)
        {
            Console.WriteLine("{0:F4}", Math.Sqrt(n));
        }

I've tried numbers through the range of values in the console, not sure what it could be.

Upvotes: 1

Views: 489

Answers (1)

Keith Nicholas
Keith Nicholas

Reputation: 44288

this gives the output thats on the site.... (your truncate one doesn't, so rounding is needed)

private static void Main(string[] args)
{            
   Console.In.ReadToEnd().Split(new[] { ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
        .Select(ulong.Parse)
        .Reverse()
        .Select(n => Math.Sqrt(n).ToString("F4"))
        .ToList()
        .ForEach(Console.WriteLine);
}

Upvotes: 8

Related Questions