ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1001. Reverse Root

1001, C#, Symbolic input, wrong answer while test 3
Posted by PROger4everPublic 14 Oct 2012 02:43
I try to parse digits by symbols this way:
using System;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Collections.Generic; //Version 2

namespace CSharpV1
{
    class Program
    {
        //static readonly char[] splitChars = { ' ', '\t', '\n', '\r' }; //Version 1
        static void Main(string[] args)
        {
            var nfi = NumberFormatInfo.InvariantInfo;
            //Version 4. Symbolic input
            int curCharDigit;
            char curChar;
            var curDigitString = String.Empty;
            var rootsStack = new Stack<double>();
            while ((curCharDigit = Console.Read()) != -1)
            {
                curChar = Convert.ToChar(curCharDigit);
                if (char.IsDigit(curChar) || curChar == '.' || curChar == ',')
                {
                    curDigitString += curChar;
                }
                else
                {
                    if (curDigitString.Length != 0)
                    {
                        var curDigit = Convert.ToDouble(curDigitString, nfi);
                        var root = Math.Sqrt(curDigit);
                        rootsStack.Push(root);

                        curDigitString = String.Empty;
                    }
                }
            }
            while (rootsStack.Count != 0)
            {
                var curRootString = string.Format(nfi, "{0:F4}", rootsStack.Pop());
                Console.WriteLine(curRootString);
            }
            rootsStack = null;
        }
    }
}

And I get Wrong answer while test 3...
The code works fine with example input data. The digits are Integer.
What's wrong?
Re: 1001, C#, Symbolic input, wrong answer while test 3
Posted by PROger4everPublic 14 Oct 2012 03:13
I've got it. I didn't parse the last number when the last symbol is digit. Corrected code:

using System;
using System.Globalization;
using System.Collections.Generic;

namespace CSharpV1
{
    class Program
    {
        static readonly NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo;
        static void Main(string[] args)
        {
            //Version 4. Symbolic input
            int curCharDigit;
            char curChar;
            var curDigitString = String.Empty;
            var rootsStack = new Stack<double>();
            while ((curCharDigit = Console.Read()) != -1)
            {
                curChar = Convert.ToChar(curCharDigit);
                if (char.IsDigit(curChar))
                {
                    curDigitString += curChar;
                }
                else
                {
                    ParseAndAdd(ref curDigitString, rootsStack);
                }
            }
            ParseAndAdd(ref curDigitString, rootsStack);
            while (rootsStack.Count != 0)
            {
                var curRootString = string.Format(nfi, "{0:F4}", rootsStack.Pop());
                Console.WriteLine(curRootString);
            }
            rootsStack = null;
        }

        static void ParseAndAdd(ref string curDigitString, Stack<double> rootsStack)
        {
            if (curDigitString.Length != 0)
            {
                var curDigit = Convert.ToDouble(curDigitString, nfi);
                var root = Math.Sqrt(curDigit);
                rootsStack.Push(root);

                curDigitString = String.Empty;
            }
        }
    }
}

Edited by author 14.10.2012 03:14