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 1068. Sum

What's wrong in this? (C#)
Posted by gholamali 29 Jan 2014 00:22
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#)
Posted by Nafiul Islam 4 Oct 2016 22:25
(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