|
|
back to boardDiscussion of Problem 1068. SumWhat's wrong in this? (C#) using System; namespace gholamali { class Program { static void Main(string[] args) { try { int N = Convert.ToInt32(Console.ReadLine()); if (Math.Abs(N) <= 10000) { if (N > 1) Console.Write(((N + 1) / 2) * N); else Console.Write(((N + 1) / 2) * (2 - N)); } Console.ReadKey(); } catch { } } } } Re: What's wrong in this? (C#) Posted by xakpc 15 Jan 2015 19:02 [TestCase("2", "3")] ((N + 1) / 2) * N when you divide on int (= 2) it produce int result (= 1) and total sum is wrong (= 2) Re: What's wrong in this? (C#) (N + 1) / 2) * N First multiply and then divide by 2 Like (N + 1) *N) / 2 Otherwise it may not act like an integer. and for second case ((N + 1) * (2 - N)) / 2 Edited by author 04.10.2016 22:27 |
|
|