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

Wrong answer test 2 on c++
Posted by TIU_Sarexer 23 Oct 2016 12:57
#include <iostream>
using namespace std;
int main() {
    int n, res = 0;
    cin >> n;
    if (n < 0) {
        for (int i = -2; i >= n; i--) {
            res = res + i;
            }
        cout << res;
    }
    else {
        for (int i = 1; i <= n; i++) {
            res = res + i;
        }
        cout << res;
    }
    return 0;
}
Re: Wrong answer test 2 on c++
Posted by German 23 Oct 2016 16:47
int i = -2; i >= n; i--    i = 1
Re: Wrong answer test 2 on c++
Posted by ToadMonster 23 Oct 2016 19:58
Optimization trick with initial i=-2 is funny.
But now you aren't processing case "n == 0" correctly.
Re: Wrong answer test 2 on c++
Posted by RENT 10 Jan 2017 11:05
if(n == 0) --> ans = ? bro.. i don't understand... i'm from vietnam
Re: Wrong answer test 2 on c++
Posted by ToadMonster 10 Jan 2017 14:46
Lets read task:
> sum of all integer numbers lying between 1 and N inclusive

If N==0 then set of integers to sum is [0..1], sum is 1.