|
|
вернуться в форум[C#] Looks right but it's wrong Can any one see what's the issue? static void Main(string[] args) { string input = Console.In.ReadToEnd(); var valid = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }; var sb = new StringBuilder(); var numbers = new Stack<string>(); for (int i = 0; i < input.Length; i++ ) { if(valid.Contains(input[i])) { sb.Append(input[i]); } else if(sb.Length > 0) { numbers.Push(sb.ToString()); sb.Clear(); } } NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo; foreach(var n in numbers) { double sqrt = Math.Sqrt(double.Parse(n, nfi)); Console.WriteLine(string.Format(nfi, "{0:F4}", sqrt)); } } Re: [C#] Looks right but it's wrong . Edited by author 16.01.2015 20:50 Re: [C#] Looks right but it's wrong If the input stream ends with a digit, your code will lose it. For debugging - replace Console.In.ReadToEnd with Console.ReadLine and try to read string with one symbol "1" |
|
|