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

why wrong answer
Posted by Abduraxmon 6 Apr 2022 07:00
#include <iostream>
using namespace std;

int sum(int a) {
    return ((1 + a)/2)*a;
}

int main () {
    int a;

    cin >> a;

    if (a > 0) cout << sum(a);
    else if (a < 0) cout << sum(abs(a)) * -1 + 1;
    else cout << 0;

    return 0;
}
Re: why wrong answer
Posted by Ehab 20 Nov 2023 02:23
if N = 0 then you need add one
so the correct solution is:

if (a > 0) cout << sum(a);
    else if (a < 0) cout << sum(abs(a)) * -1 + 1;
    else cout << 1;