ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1001. Обратный корень

1001, C#, Symbolic input, wrong answer while test 3
Послано PROger4everPublic 14 окт 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
Послано PROger4everPublic 14 окт 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